home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / PowerLisp 2.01 / Supplemental Documentation / Documentation / Chapter 29. Conditions < prev    next >
Text File  |  1995-03-28  |  128KB  |  3,077 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 29. Conditions
  5.  
  6. By Kent M. Pitman
  7.  
  8. [change_begin]
  9. PREFACE: The language defined by the first edition contained an enormous
  10. lacuna: although facilities were specified for signaling errors, no means was
  11. defined for handling errors. This occurred not through neglect of the issue,
  12. but because this part of the Lisp language generally was in a state of flux.
  13. There were several proposals at the time. The committee, finding that it could
  14. not agree on any one proposal, agreed to disagree and omit error handling from
  15. Common Lisp for the time being. This defect has now been addressed.
  16.  
  17. X3J13 voted in June 1988 (CONDITION-SYSTEM)   to adopt the Common Lisp
  18. Condition System as a part of the forthcoming draft Common Lisp standard. X3J13
  19. voted in March 1989 (ZLOS-CONDITIONS)   to amend the specification of
  20. conditions to integrate them with the Common Lisp Object System (see chapter
  21. 28). X3J13 voted in June 1989 (CONDITION-RESTARTS)   to amend the specification
  22. of restarts in certain ways. These amendments have been incorporated here with
  23. little further comment.
  24.  
  25. This chapter presents the bulk of the Common Lisp Condition System proposal,
  26. written by Kent M. Pitman and amended by X3J13. I have edited it only very
  27. lightly to conform to the overall style of this book and have inserted a small
  28. number of bracketed remarks identified by the initials GLS. Please see the
  29. Acknowledgments to this second edition for the author's acknowledgments to
  30. others who contributed to the Condition System proposal.
  31.  
  32.      -Guy L. Steele Jr.
  33.  
  34. [change_end]
  35. -------------------------------------------------------------------------------
  36.  
  37.    *  Introduction
  38.    *  Changes in Terminology
  39.    *  Survey of Concepts
  40.         o  Signaling Errors
  41.         o  Trapping Errors
  42.         o  Handling Conditions
  43.         o  Object-Oriented Basis of Condition Handling
  44.         o  Restarts
  45.         o  Anonymous Restarts
  46.         o  Named Restarts
  47.         o  Restart Functions
  48.         o  Comparison of Restarts and Catch/Throw
  49.         o  Generalized Restarts
  50.         o  Interactive Condition Handling
  51.         o  Serious Conditions
  52.         o  Non-Serious Conditions
  53.         o  Condition Types
  54.         o  Signaling Conditions
  55.         o  Resignaling Conditions
  56.         o  Condition Handlers
  57.         o  Printing Conditions
  58.    *  Program Interface to the Condition System
  59.         o  Signaling Conditions
  60.         o  Assertions
  61.         o  Exhaustive Case Analysis
  62.         o  Handling Conditions
  63.         o  Defining Conditions
  64.         o  Creating Conditions
  65.         o  Establishing Restarts
  66.         o  Finding and Manipulating Restarts
  67.         o  Warnings
  68.         o  Restart Functions
  69.         o  Debugging Utilities
  70.    *  Predefined Condition Types
  71.  
  72. -------------------------------------------------------------------------------
  73.  
  74. 29.1. Introduction
  75.  
  76. [change_begin]
  77. Often we find it useful to describe a function in terms of its behavior in
  78. ``normal situations.'' For example, we may say informally that the function +
  79. returns the sum of its arguments or that the function read-char returns the
  80. next available character on a given input stream.
  81.  
  82. Sometimes, however, an ``exceptional situation'' will arise that does not fit
  83. neatly into such descriptions. For example, + might receive an argument that is
  84. not a number, or read-char might receive as a single argument a stream that has
  85. no more available characters. This distinction between normal and exceptional
  86. situations is in some sense arbitrary but is often very useful in practice.
  87.  
  88. For example, suppose a function f were defined to allow only integer arguments
  89. but also guaranteed to detect and signal an error for non-integer arguments.
  90. Such a description is in fact internally inconsistent (that is, paradoxical)
  91. because the function's behavior is well-defined for non-integers. Yet we would
  92. not want this annoying paradox to force description of f as a function that
  93. accepts any kind of argument (just in case f is being called only as a quick
  94. way to signal an error, for example). Using the normal/exceptional distinction,
  95. we can say clearly that f accepts integers in the normal situation and signals
  96. an error in exceptional situations. Moreover, we can say that when we refer to
  97. the definition of a function informally, it is acceptable to speak only of its
  98. normal behavior. For example, we can speak informally about f as a function
  99. that accepts only integers without feeling that we are committing some awful
  100. fraud.
  101.  
  102. Not all exceptional situations are errors. For example, a program that is
  103. directing the typing of a long line of text may come to an end-of-line. It is
  104. possible that no real harm will result from failing to signal end-of-line to
  105. its caller because the operating system will simply force a carriage return on
  106. the output device, which will continue typing on the next line. However, it may
  107. still be interesting to establish a protocol whereby the printing program can
  108. inform its caller of end-of-line exceptions. The caller could then opt to deal
  109. with these situations in interesting ways at certain times. For example, a
  110. caller might choose to terminate printing, obtaining an end-of-line truncation.
  111. The important thing, however, is that the failure of the caller to provide
  112. advice about the situation need not prevent the printer program from operating
  113. correctly.
  114.  
  115. Mechanisms for dealing with exceptional situations vary widely. When an
  116. exceptional situation is encountered, a program may attempt to handle it by
  117. returning a distinguished value, returning an additional value, setting a
  118. variable, calling a function, performing a special transfer of control, or
  119. stopping the program altogether and entering the debugger.
  120.  
  121. For the most part, the facilities described in this chapter do not introduce
  122. any fundamentally new way of dealing with exceptional situations. Rather, they
  123. encapsulate and formalize useful patterns of data and control flow that have
  124. been seen to be useful in dealing with exceptional situations.
  125.  
  126. A proper conceptual approach to errors should perhaps begin from first
  127. principles, with a discussion of conditions in general, and eventually work up
  128. to the concept of an error as just one of the many kinds of conditions.
  129. However, given the primitive state of error-handling technology, a proper
  130. buildup may be as inappropriate as requiring that a beggar learn to cook a
  131. gourmet meal before being allowed to eat. Thus, we deal first with the
  132. essentials-error handling-and then go back later to fill in the missing
  133. details.
  134. [change_end]
  135.  
  136. -------------------------------------------------------------------------------
  137. 29.2. Changes in Terminology
  138.  
  139. [change_begin]
  140. In this section, we introduce changes to the terminology defined in section
  141. 1.2.4.
  142.  
  143. A condition is an interesting situation in a program that has been detected and
  144. announced. Later we allow this term also to refer to objects that programs use
  145. to represent such situations.
  146.  
  147. An error is a condition in which normal program execution may not continue
  148. without some form of intervention (either interactively by the user or under
  149. some sort of program control, as described below).
  150.  
  151. The process by which a condition is formally announced by a program is called
  152. signaling. The function signal is the primitive mechanism by which such
  153. announcement is done. Other abstractions, such as error and cerror, are built
  154. using signal.
  155.  
  156. The first edition is ambiguous about the reason why a particular program action
  157. ``is an error.'' There are two principal reasons why an action may be an error
  158. without being required to signal an error:
  159.  
  160.    *  Detecting the error might be prohibitively expensive.
  161.  
  162.      For example, (+ nil 3) is an error. It is likely that the designers of
  163.      Common Lisp believed this would be an error in all implementations but
  164.      felt it might be excessively expensive to detect the problem in compiled
  165.      code on stock hardware, so they did not require that it signal an error.
  166.  
  167.    *  Some implementations might implement the behavior as an extension.
  168.  
  169.      For example, (loop for x from 1 to 3 do (print x)) is an error because
  170.      loop is not defined to take atoms in its body. In fact, however, some
  171.      implementations offer an extension that makes this well-defined. In order
  172.      to leave room for such extensions, the first edition used the ``is an
  173.      error'' terminology to keep implementors from being forced to signal an
  174.      error in the extended implementations.
  175.  
  176.      [This example was written well before the vote by X3J13 in January 1989 to
  177.      add exactly this extension to the forthcoming draft standard (see chapter
  178.      26).-GLS]
  179.  
  180. In this chapter, we use the following terminology. [Compare this to the
  181. terminology presented in section 28.1.1.-GLS]
  182.  
  183.    *  If the signaling of a condition or error is part of a function's contract
  184.      in all situations, we say that it ``signals'' or ``must signal'' that
  185.      condition or error.
  186.  
  187.    *  If the signaling of a condition or error is optional for some important
  188.      reason (such as performance), we say that the program ``might signal''
  189.      that condition or error. In this case, we are defining the operation to be
  190.      illegal in all implementations, but allowing some implementations to fail
  191.      to detect the error.
  192.  
  193.    *  If an action is left undefined for the sake of implementation-dependent
  194.      extension, we say that it ``is undefined'' or ``has undefined effect.''
  195.      This means that it is not possible to depend portably upon the effects of
  196.      that action. A program that has undefined effect may enter the debugger,
  197.      transfer control, or modify data in unpredictable ways.
  198.  
  199.    *  In the special case where only the return value of an operation is not
  200.      well defined but any side effect and transfer-of-control behavior is well
  201.      defined, we say that it has ``undefined value.'' In this case, the number
  202.      and nature of the return values is not defined, but the function can
  203.      reasonably be expected to return. It is worth noting that under this
  204.      description, there are some (though not many) legitimate ways in which
  205.      such return value(s) can be used. For example, if the function foo has no
  206.      side effects and undefined value, the expression (length (list (foo))) is
  207.      completely well defined even for portable code. However, the effect of
  208.      (print (list (foo))) is not well defined.
  209.  
  210. [change_end]
  211.  
  212. -------------------------------------------------------------------------------
  213.  
  214. 29.3. Survey of Concepts
  215.  
  216. [change_begin]
  217. This section discusses various aspects of the condition system by topic,
  218. illustrating them with extensive examples. The next section contains
  219. definitions of specific functions, macros, and other facilities.
  220. [change_end]
  221.  
  222. -------------------------------------------------------------------------------
  223.  
  224.    *  Signaling Errors
  225.    *  Trapping Errors
  226.    *  Handling Conditions
  227.    *  Object-Oriented Basis of Condition Handling
  228.    *  Restarts
  229.    *  Anonymous Restarts
  230.    *  Named Restarts
  231.    *  Restart Functions
  232.    *  Comparison of Restarts and Catch/Throw
  233.    *  Generalized Restarts
  234.    *  Interactive Condition Handling
  235.    *  Serious Conditions
  236.    *  Non-Serious Conditions
  237.    *  Condition Types
  238.    *  Signaling Conditions
  239.    *  Resignaling Conditions
  240.    *  Condition Handlers
  241.    *  Printing Conditions
  242.  
  243. -------------------------------------------------------------------------------
  244.  
  245. 29.3.1. Signaling Errors
  246.  
  247. [change_begin]
  248. Conceptually, signaling an error in a program is an admission by that program
  249. that it does not know how to continue and requires external intervention. Once
  250. an error is signaled, any decision about how to continue must come from the
  251. ``outside.''
  252.  
  253. The simplest way to signal an error is to use the error function with
  254. format-style arguments describing the error for the sake of the user interface.
  255. If error is called and there are no active handlers (described in sections
  256. 29.3.2 and 29.3.3), the debugger will be entered and the error message will be
  257. typed out. For example:
  258.  
  259. Lisp> (defun factorial (x)
  260.         (cond ((or (not (typep x 'integer)) (minusp x))
  261.                (error "~S is not a valid argument to FACTORIAL."
  262.                       x))
  263.               ((zerop x) 1)
  264.               (t (* x (factorial (- x 1))))))
  265.  => FACTORIAL
  266. Lisp> (factorial 20)
  267.  => 2432902008176640000
  268. Lisp> (factorial -1)
  269. Error: -1 is not a valid argument to FACTORIAL.
  270. To continue, type :CONTINUE followed by an option number:
  271.  1: Return to Lisp Toplevel.
  272. Debug>
  273.  
  274. In general, a call to error cannot directly return. Unless special work has
  275. been done to override this behavior, the debugger will be entered and there
  276. will be no option to simply continue.
  277.  
  278. The only exception may be that some implementations may provide debugger
  279. commands for interactively returning from individual stack frames; even then,
  280. however, such commands should never be used except by someone who has read the
  281. erring code and understands the consequences of continuing from that point. In
  282. particular, the programmer should feel confident about writing code like this:
  283.  
  284. (defun wargames:no-win-scenario ()
  285.   (when (true) (error "Pushing the button would be stupid."))
  286.   (push-the-button))
  287.  
  288. In this scenario, there should be no chance that the function error will return
  289. and the button will be pushed.
  290.  
  291. -------------------------------------------------------------------------------
  292. Remark: It should be noted that the notion of ``no chance'' that the button
  293. will be pushed is relative only to the language model; it assumes that the
  294. language is accurately implemented. In practice, compilers have bugs, computers
  295. have glitches, and users have been known to interrupt at inopportune moments
  296. and use the debugger to return from arbitrary stack frames. Such violations of
  297. the language model are beyond the scope of the condition system but not
  298. necessarily beyond the scope of potential failures that the programmer should
  299. consider and defend against. The possibility of such unusual failures may of
  300. course also influence the design of code meant to handle less drastic
  301. situations, such as maintaining a database uncorrupted.-KMP and GLS
  302. -------------------------------------------------------------------------------
  303.  
  304. In some cases, the programmer may have a single, well-defined idea of a
  305. reasonable recovery strategy for this particular error. In that case, he can
  306. use the function cerror, which specifies information about what would happen if
  307. the user did simply continue from the call to cerror. For example:
  308.  
  309. Lisp> (defun factorial (x)
  310.         (cond ((not (typep x 'integer))
  311.                (error "~S is not a valid argument to FACTORIAL."
  312.                       x))
  313.               ((minusp x)
  314.                (let ((x-magnitude (- x)))
  315.                  (cerror "Compute -(~D!) instead."
  316.                          "(-~D)! is not defined." x-magnitude)
  317.                  (- (factorial x-magnitude))))
  318.               ((zerop x) 1)
  319.               (t (* x (factorial (- x 1))))))
  320.  => FACTORIAL
  321. Lisp> (factorial -3)
  322. Error: (-3)! is not defined.
  323. To continue, type :CONTINUE followed by an option number:
  324.  1: Compute -(3!) instead.
  325.  2: Return to Lisp Toplevel.
  326. Debug> :continue 1
  327.  => -6
  328.  
  329. [change_end]
  330.  
  331. -------------------------------------------------------------------------------
  332.  
  333. 29.3.1. Signaling Errors
  334.  
  335. [change_begin]
  336. Conceptually, signaling an error in a program is an admission by that program
  337. that it does not know how to continue and requires external intervention. Once
  338. an error is signaled, any decision about how to continue must come from the
  339. ``outside.''
  340.  
  341. The simplest way to signal an error is to use the error function with
  342. format-style arguments describing the error for the sake of the user interface.
  343. If error is called and there are no active handlers (described in sections
  344. 29.3.2 and 29.3.3), the debugger will be entered and the error message will be
  345. typed out. For example:
  346.  
  347. Lisp> (defun factorial (x)
  348.         (cond ((or (not (typep x 'integer)) (minusp x))
  349.                (error "~S is not a valid argument to FACTORIAL."
  350.                       x))
  351.               ((zerop x) 1)
  352.               (t (* x (factorial (- x 1))))))
  353.  => FACTORIAL
  354. Lisp> (factorial 20)
  355.  => 2432902008176640000
  356. Lisp> (factorial -1)
  357. Error: -1 is not a valid argument to FACTORIAL.
  358. To continue, type :CONTINUE followed by an option number:
  359.  1: Return to Lisp Toplevel.
  360. Debug>
  361.  
  362. In general, a call to error cannot directly return. Unless special work has
  363. been done to override this behavior, the debugger will be entered and there
  364. will be no option to simply continue.
  365.  
  366. The only exception may be that some implementations may provide debugger
  367. commands for interactively returning from individual stack frames; even then,
  368. however, such commands should never be used except by someone who has read the
  369. erring code and understands the consequences of continuing from that point. In
  370. particular, the programmer should feel confident about writing code like this:
  371.  
  372. (defun wargames:no-win-scenario ()
  373.   (when (true) (error "Pushing the button would be stupid."))
  374.   (push-the-button))
  375.  
  376. In this scenario, there should be no chance that the function error will return
  377. and the button will be pushed.
  378.  
  379. -------------------------------------------------------------------------------
  380. Remark: It should be noted that the notion of ``no chance'' that the button
  381. will be pushed is relative only to the language model; it assumes that the
  382. language is accurately implemented. In practice, compilers have bugs, computers
  383. have glitches, and users have been known to interrupt at inopportune moments
  384. and use the debugger to return from arbitrary stack frames. Such violations of
  385. the language model are beyond the scope of the condition system but not
  386. necessarily beyond the scope of potential failures that the programmer should
  387. consider and defend against. The possibility of such unusual failures may of
  388. course also influence the design of code meant to handle less drastic
  389. situations, such as maintaining a database uncorrupted.-KMP and GLS
  390. -------------------------------------------------------------------------------
  391.  
  392. In some cases, the programmer may have a single, well-defined idea of a
  393. reasonable recovery strategy for this particular error. In that case, he can
  394. use the function cerror, which specifies information about what would happen if
  395. the user did simply continue from the call to cerror. For example:
  396.  
  397. Lisp> (defun factorial (x)
  398.         (cond ((not (typep x 'integer))
  399.                (error "~S is not a valid argument to FACTORIAL."
  400.                       x))
  401.               ((minusp x)
  402.                (let ((x-magnitude (- x)))
  403.                  (cerror "Compute -(~D!) instead."
  404.                          "(-~D)! is not defined." x-magnitude)
  405.                  (- (factorial x-magnitude))))
  406.               ((zerop x) 1)
  407.               (t (* x (factorial (- x 1))))))
  408.  => FACTORIAL
  409. Lisp> (factorial -3)
  410. Error: (-3)! is not defined.
  411. To continue, type :CONTINUE followed by an option number:
  412.  1: Compute -(3!) instead.
  413.  2: Return to Lisp Toplevel.
  414. Debug> :continue 1
  415.  => -6
  416.  
  417. [change_end]
  418.  
  419. -------------------------------------------------------------------------------
  420.  
  421. 29.3.2. Trapping Errors
  422.  
  423. [change_begin]
  424. By default, a call to error will force entry into the debugger. You can
  425. override that behavior in a variety of ways. The simplest (and most blunt) tool
  426. for inhibiting entry to the debugger on an error is to use ignore-errors. In
  427. the normal situation, forms in the body of ignore-errors are evaluated
  428. sequentially and the last value is returned. If a condition of type error is
  429. signaled, ignore-errors immediately returns two values, namely nil and the
  430. condition that was signaled; the debugger is not entered and no error message
  431. is printed. For example:
  432.  
  433. Lisp> (setq filename "nosuchfile")
  434.  => "nosuchfile"
  435. Lisp> (ignore-errors (open filename :direction :input))
  436.  => NIL and #<FILE-ERROR 3437523>
  437.  
  438. The second return value is an object that represents the kind of error. This is
  439. explained in greater detail in section 29.3.4.
  440.  
  441. In many cases, however, ignore-errors is not desirable because it deals with
  442. too many kinds of errors. Contrary to the belief of some, a program that does
  443. not enter the debugger is not necessarily better than one that does. Excessive
  444. use of ignore-errors may keep the program out of the debugger, but it may not
  445. increase the program's reliability, because the program may continue to run
  446. after encountering errors other than those you meant to work past. In general,
  447. it is better to attempt to deal only with the particular kinds of errors that
  448. you believe could legitimately happen. That way, if an unexpected error comes
  449. along, you will still find out about it.
  450.  
  451. ignore-errors is a useful special case built from a more general facility,
  452. handler-case, that allows the programmer to deal with particular kinds of
  453. conditions (including non-error conditions) without affecting what happens when
  454. other kinds of conditions are signaled. For example, an effect equivalent to
  455. that of ignore-errors above is achieved in the following example:
  456.  
  457. Lisp> (setq filename "nosuchfile")
  458.  => "nosuchfile"
  459. Lisp> (handler-case (open filename :direction :input)
  460.         (error (condition)
  461.           (values nil condition)))
  462.  => NIL and #<FILE-ERROR 3437525>
  463.  
  464. However, using handler-case, one can indicate a more specific condition type
  465. than just ``error.'' Condition types are explained in detail later, but the
  466. syntax looks roughly like the following:
  467.  
  468. Lisp> (makunbound 'filename)
  469.  => FILENAME
  470. Lisp> (handler-case (open filename :direction :input)
  471.         (file-error (condition)
  472.           (values nil condition)))
  473. Error: The variable FILENAME is unbound.
  474. To continue, type :CONTINUE followed by an option number:
  475.  1: Retry getting the value of FILENAME.
  476.  2: Specify a value of FILENAME to use this time.
  477.  3: Specify a value of FILENAME to store and use.
  478.  4: Return to Lisp Toplevel.
  479. Debug>
  480.  
  481. [change_end]
  482.  
  483. -------------------------------------------------------------------------------
  484.  
  485. 29.3.3. Handling Conditions
  486.  
  487. [change_begin]
  488. Blind transfer of control to a handler-case is only one possible kind of
  489. recovery action that can be taken when a condition is signaled. The low-level
  490. mechanism offers great flexibility in how to continue once a condition has been
  491. signaled.
  492.  
  493. The basic idea behind condition handling is that a piece of code called the
  494. signaler recognizes and announces the existence of an exceptional situation
  495. using signal or some function built on signal (such as error).
  496.  
  497. The process of signaling involves the search for and invocation of a handler, a
  498. piece of code that will attempt to deal appropriately with the situation.
  499.  
  500. If a handler is found, it may either handle the situation, by performing some
  501. non-local transfer of control, or decline to handle it, by failing to perform a
  502. non-local transfer of control. If it declines, other handlers are sought.
  503.  
  504. Since the lexical environment of the signaler might not be available to
  505. handlers, a data structure called a condition is created to represent
  506. explicitly the relevant state of the situation. A condition either is created
  507. explicitly using make-condition and then passed to a function such as signal,
  508. or is created implicitly by a function such as signal when given appropriate
  509. non-condition arguments.
  510.  
  511. In order to handle the error, a handler is permitted to use any non-local
  512. transfer of control such as go to a tag in a tagbody, return from a block, or
  513. throw to a catch. In addition, structured abstractions of these primitives are
  514. provided for convenience in exception handling.
  515.  
  516. A handler can be made dynamically accessible to a program by use of
  517. handler-bind. For example, to create a handler for a condition of type
  518. arithmetic-error, one might write:
  519.  
  520. (handler-bind ((arithmetic-error handler))body)
  521.  
  522. The handler is a function of one argument, the condition. If a condition of the
  523. designated type is signaled while the body is executing (and there are no
  524. intervening handlers), the handler would be invoked on the given condition,
  525. allowing it the option of transferring control. For example, one might write a
  526. macro that executes a body, returning either its value(s) or the two values nil
  527. and the condition:
  528.  
  529. (defmacro without-arithmetic-errors (&body forms)
  530.   (let ((tag (gensym)))
  531.     `(block ,tag
  532.        (handler-bind ((arithmetic-error
  533.                          #'(lambda (c)     ;Argument c is a condition
  534.                              (return-from ,tag (values nil c)))))
  535.          ,@body))))
  536.  
  537. The handler is executed in the dynamic context of the signaler, except that the
  538. set of available condition handlers will have been rebound to the value that
  539. was active at the time the condition handler was made active. If a handler
  540. declines (that is, it does not transfer control), other handlers are sought. If
  541. no handler is found and the condition was signaled by error or cerror (or some
  542. function such as assert that behaves like these functions), the debugger is
  543. entered, still in the dynamic context of the signaler.
  544. [change_end]
  545.  
  546. -------------------------------------------------------------------------------
  547.  
  548. 29.3.4. Object-Oriented Basis of Condition Handling
  549.  
  550. [change_begin]
  551. Of course, the ability of the handler to usefully handle an exceptional
  552. situation is related to the quality of the information it is provided. For
  553. example, if all errors were signaled by
  554.  
  555. (error "some format string")
  556.  
  557. then the only piece of information that would be accessible to the handler
  558. would be an object of type simple-error that had a slot containing the format
  559. string.
  560.  
  561. If this were done, string-equal would be the preferred way to tell one error
  562. from another, and it would be very hard to allow flexibility in the
  563. presentation of error messages because existing handlers would tend to be
  564. broken by even tiny variations in the wording of an error message. This
  565. phenomenon has been the major failing of most error systems previously
  566. available in Lisp. It is fundamentally important to decouple the error message
  567. string (the human interface) from the objects that formally represent the error
  568. state (the program interface). We therefore have the notion of typed
  569. conditions, and of formal operations on those conditions that make them
  570. inspectable in a structured way.
  571.  
  572. This object-oriented approach to condition handling has the following important
  573. advantages over a text-based approach:
  574.  
  575.    *  Conditions are classified according to subtype relationships, making it
  576.      easy to test for categories of conditions.
  577.  
  578.    *  Conditions have named slot values through which parameters are conveyed
  579.      from the program that signals the condition to the program that handles
  580.      it.
  581.  
  582.    *  Inheritance of methods and slots reduces the amount of explicit
  583.      specification necessary to achieve various interesting effects.
  584.  
  585. Some condition types are defined by this document, but the set of condition
  586. types is extensible using define-condition. Common Lisp condition types are in
  587. fact CLOS classes, and condition objects are ordinary CLOS objects;
  588. define-condition merely provides an abstract interface that is a bit more
  589. convenient than defclass for defining conditions.
  590.  
  591. Here, as an example, we define a two-argument function called divide that is
  592. patterned after the / function but does some stylized error checking:
  593.  
  594. (defun divide (numerator denominator)
  595.   (cond ((or (not (numberp numerator))
  596.              (not (numberp denominator)))
  597.          (error "(DIVIDE '~S '~S) - Bad arguments."
  598.                 numerator denominator))
  599.         ((zerop denominator)
  600.          (error 'division-by-zero
  601.                 :operator 'divide
  602.                 :operands (list numerator denominator)))
  603.         (t ...)))
  604.  
  605. Note that in the first clause we have used error with a string argument and in
  606. the second clause we have named a particular condition type, division-by-zero.
  607. In the case of a string argument, the condition type that will be signaled is
  608. simple-error.
  609.  
  610. The particular kind of error that is signaled may be important in cases where
  611. handlers are active. For example, simple-error inherits from type error, which
  612. in turn inherits from type condition. On the other hand, division-by-zero
  613. inherits from arithmetic-error, which inherits from error, which inherits from
  614. condition. So if a handler existed for arithmetic-error while a
  615. division-by-zero condition was signaled, that handler would be tried; however,
  616. if a simple-error condition were signaled in the same context, the handler for
  617. type arithmetic-error would not be tried.
  618. [change_end]
  619.  
  620. -------------------------------------------------------------------------------
  621.  
  622. 29.3.5. Restarts
  623.  
  624. [change_begin]
  625. In older Lisp dialects (such as MacLisp), an attempt to signal an error of a
  626. given type often carried with it an implicit promise to support the standard
  627. recovery strategy for that type of error. If the signaler knew the type of
  628. error but for whatever reason was unable to deal with the standard recovery
  629. strategy for that kind of error, it was necessary to signal an untyped error
  630. (for which there was no defined recovery strategy). This sometimes led to
  631. confusion when people signaled typed errors without realizing the full
  632. implications of having done so, but more often than not it meant that users
  633. simply avoided typed errors altogether.
  634.  
  635. The Common Lisp Condition System, which is modeled after the Zetalisp condition
  636. system, corrects this troublesome aspect of previous Lisp dialects by creating
  637. a clear separation between the act of signaling an error of a particular type
  638. and the act of saying that a particular way of recovery is appropriate. In the
  639. divide example above, simply signaling an error does not imply a willingness on
  640. the part of the signaler to cooperate in any corrective action. For example,
  641. the following sample interaction illustrates that the only recovery action
  642. offered for this error is ``Return to Lisp Toplevel'':
  643.  
  644. Lisp> (+ (divide 3 0) 7)
  645. Error: Attempt to divide 3 by 0.
  646. To continue, type :CONTINUE followed by an option number:
  647.  1: Return to Lisp Toplevel.
  648. Debug> :continue 1
  649. Returned to Lisp Toplevel.
  650. Lisp>
  651.  
  652. When an error is detected and the function error is called, execution cannot
  653. continue normally because error will not directly return. Control can be
  654. transferred to other points in the program, however, by means of specially
  655. established ``restarts.''
  656. [change_end]
  657.  
  658. -------------------------------------------------------------------------------
  659.  
  660. 29.3.6. Anonymous Restarts
  661.  
  662. [change_begin]
  663. The simplest kind of restart involves structured transfer of control using a
  664. macro called restart-case. The restart-case form allows execution of a piece of
  665. code in a context where zero or more restarts are active, and where if one of
  666. those restarts is ``invoked,'' control will be transferred to the corresponding
  667. clause in the restart-case form. For example, we could rewrite the previous
  668. divide example as follows.
  669.  
  670. (defun divide (numerator denominator)
  671.   (loop
  672.     (restart-case
  673.         (return
  674.           (cond ((or (not (numberp numerator))
  675.                      (not (numberp denominator)))
  676.                  (error "(DIVIDE '~S '~S) - Bad arguments."
  677.                          numerator denominator))
  678.                 ((zerop denominator)
  679.                  (error 'division-by-zero
  680.                         :operator 'divide
  681.                         :operands (list numerator denominator)))
  682.                 (t ...)))
  683.       (nil (arg1 arg2)
  684.           :report "Provide new arguments for use by DIVIDE."
  685.           :interactive
  686.             (lambda ()
  687.                (list (prompt-for 'number "Numerator: ")
  688.                      (prompt-for 'number "Denominator: ")))
  689.         (setq numerator arg1 denominator arg2))
  690.       (nil (result)
  691.           :report "Provide a value to return from DIVIDE."
  692.           :interactive
  693.             (lambda () (list (prompt-for 'number "Result: ")))
  694.         (return result)))))
  695.  
  696. -------------------------------------------------------------------------------
  697. Remark: The function prompt-for used in this chapter in a number of places is
  698. not a part of Common Lisp. It is used in the examples in this chapter only to
  699. keep the presentation simple. It is assumed to accept a type specifier and
  700. optionally a format string and associated arguments. It uses the format string
  701. and associated arguments as part of an interactive prompt, and uses read to
  702. read a Lisp object; however, only an object of the type indicated by the type
  703. specifier is accepted.
  704.  
  705. The question of whether or not prompt-for (or something like it) would be a
  706. useful addition to Common Lisp is under consideration by X3J13, but as of
  707. January 1989 no action has been taken. In spite of its use in a number of
  708. examples, nothing in the Common Lisp Condition System depends on this function.
  709. -------------------------------------------------------------------------------
  710.  
  711. In the example, the nil at the head of each clause means that it is an
  712. ``anonymous'' restart. Anonymous restarts are typically invoked only from
  713. within the debugger. As we shall see later, it is possible to have ``named
  714. restarts'' that may be invoked from code without the need for user
  715. intervention.
  716.  
  717. If the arguments to anonymous restarts are not optional, then special
  718. information must be provided about what the debugger should use as arguments.
  719. Here the :interactive keyword is used to specify that information.
  720.  
  721. The :report keyword introduces information to be used when presenting the
  722. restart option to the user (by the debugger, for example).
  723.  
  724. Here is a sample interaction that takes advantage of the restarts provided by
  725. the revised definition of divide:
  726.  
  727. Lisp> (+ (divide 3 0) 7)
  728. Error: Attempt to divide 3 by 0.
  729. To continue, type :CONTINUE followed by an option number:
  730.  1: Provide new arguments for use by the DIVIDE function.
  731.  2: Provide a value to return from the DIVIDE function.
  732.  3: Return to Lisp Toplevel.
  733. Debug> :continue 1
  734.  
  735. Numerator: 4
  736. Denominator: 2
  737.  => 9
  738.  
  739. [change_end]
  740.  
  741. -------------------------------------------------------------------------------
  742.  
  743. 29.3.7. Named Restarts
  744.  
  745. [change_begin]
  746. In addition to anonymous restarts, one can have named restarts, which can be
  747. invoked by name from within code. As a trivial example, one could write
  748.  
  749. (restart-case (invoke-restart 'foo 3)
  750.   (foo (x) (+ x 1)))
  751.  
  752. to add 3 to 1, returning 4. This trivial example is conceptually analogous to
  753. writing:
  754.  
  755. (+ (catch 'something (throw 'something 3)) 1)
  756.  
  757. For a more realistic example, the code for the function symbol-value might
  758. signal an unbound variable error as follows:
  759.  
  760. (restart-case (error "The variable ~S is unbound." variable)
  761.   (continue ()
  762.       :report
  763.         (lambda (s)     ;Argument s is a stream
  764.           (format s "Retry getting the value of ~S." variable))
  765.     (symbol-value variable))
  766.   (use-value (value)
  767.       :report
  768.         (lambda (s)     ;Argument s is a stream
  769.           (format s "Specify a value of ~S to use this time."
  770.                   variable))
  771.     value)
  772.   (store-value (value)
  773.       :report
  774.         (lambda (s)     ;Argument s is a stream
  775.           (format s "Specify a value of ~S to store and use."
  776.                   variable))
  777.     (setf (symbol-value variable) value)
  778.     value))
  779.  
  780. If this were part of the implementation of symbol-value, then it would be
  781. possible for users to write a variety of automatic handlers for unbound
  782. variable errors. For example, to make unbound variables evaluate to themselves,
  783. one might write
  784.  
  785. (handler-bind ((unbound-variable
  786.                  #'(lambda (c)     ;Argument c is a condition
  787.                      (when (find-restart 'use-value)
  788.                        (invoke-restart 'use-value
  789.                                        (cell-error-name c))))))
  790.   body)
  791.  
  792. [change_end]
  793.  
  794. -------------------------------------------------------------------------------
  795.  
  796. 29.3.8. Restart Functions
  797.  
  798. [change_begin]
  799. For commonly used restarts, it is conventional to define a program interface
  800. that hides the use of invoke-restart. Such program interfaces to restarts are
  801. called restart functions.
  802.  
  803. The normal convention is for the function to share the name of the restart. The
  804. pre-defined functions abort, continue, muffle-warning, store-value, and
  805. use-value are restart functions. With use-value the above example of
  806. handler-bind could have been written more concisely as
  807.  
  808. (handler-bind ((unbound-variable
  809.                    #'(lambda (c)     ;Argument c is a condition
  810.                        (use-value (cell-error-name c)))))
  811.   body)
  812.  
  813. [change_end]
  814.  
  815. -------------------------------------------------------------------------------
  816.  
  817. 29.3.9. Comparison of Restarts and Catch/Throw
  818.  
  819. [change_begin]
  820. One important feature that restart-case (or restart-bind) offers that catch
  821. does not is the ability to reason about the available points to which control
  822. might be transferred without actually attempting the transfer. One could, for
  823. example, write
  824.  
  825. (ignore-errors (throw ...))
  826.  
  827. which is a sort of poor man's variation of
  828.  
  829. (when (find-restart 'something)
  830.   (invoke-restart 'something))
  831.  
  832. but there is no way to use ignore-errors and throw to simulate something like
  833.  
  834. (when (and (find-restart 'something)
  835.            (find-restart 'something-else))
  836.   (invoke-restart 'something))
  837.  
  838. or even just
  839.  
  840. (when (and (find-restart 'something)
  841.            (yes-or-no-p "Do something? "))
  842.   (invoke-restart 'something))
  843.  
  844. because the degree of inspectability that comes with simply writing
  845.  
  846. (ignore-errors (throw ...))
  847.  
  848. is too primitive-getting the desired information also forces transfer of
  849. control, perhaps at a time when it is not desirable.
  850.  
  851. Many programmers have previously evolved strategies like the following on a
  852. case-by-case basis:
  853.  
  854. (defvar *foo-tag-is-available* nil)
  855.  
  856. (defun fn-1 ()
  857.   (catch 'foo
  858.     (let ((*foo-tag-is-available* t))
  859.       ... (fn-2) ...)))
  860.  
  861. (defun fn-2 ()
  862.   ...
  863.   (if *foo-tag-is-available* (throw 'foo t))
  864.   ...)
  865.  
  866. The facility provided by restart-case and find-restart is intended to provide a
  867. standardized protocol for this sort of information to be communicated between
  868. programs that were developed independently so that individual variations from
  869. program to program do not thwart the overall modularity and debuggability of
  870. programs.
  871.  
  872. Another difference between the restart facility and the catch/throw facility is
  873. that a catch with any given tag completely shadows any outer pending catch that
  874. uses the same tag. Because of the presence of compute-restarts, however, it is
  875. possible to see shadowed restarts, which may be very useful in some situations
  876. (particularly in an interactive debugger).
  877. [change_end]
  878.  
  879. -------------------------------------------------------------------------------
  880.  
  881. 29.3.10. Generalized Restarts
  882.  
  883. [change_begin]
  884. restart-case is a mechanism that allows only imperative transfer of control for
  885. its associated restarts. restart-case is built on a lower-level mechanism
  886. called restart-bind, which does not force transfer of control.
  887.  
  888. restart-bind is to restart-case as handler-bind is to handler-case. The syntax
  889. is
  890.  
  891. (restart-bind ((name function . options)) . body)
  892.  
  893. The body is executed in a dynamic context within which the function will be
  894. called whenever (invoke-restart 'name) is executed. The options are
  895. keyword-style and are used to pass information such as that provided with the
  896. :report keyword in restart-case.
  897.  
  898. A restart-case expands into a call to restart-bind where the function simply
  899. does an unconditional transfer of control to a particular body of code, passing
  900. along ``argument'' information in a structured way.
  901.  
  902. It is also possible to write restarts that do not transfer control. Such
  903. restarts may be useful in implementing various special commands for the
  904. debugger that are of interest only in certain situations. For example, one
  905. might imagine a situation where file space was exhausted and the following was
  906. done in an attempt to free space in directory dir:
  907.  
  908. (restart-bind ((nil #'(lambda () (expunge-directory dir))
  909.                     :report-function
  910.                       #'(lambda (stream)
  911.                           (format stream "Expunge ~A."
  912.                                   (directory-namestring dir)))))
  913.   (cerror "Try this file operation again."
  914.           'directory-full :directory dir))
  915.  
  916. In this case, the debugger might be entered and the user could first perform
  917. the expunge (which would not transfer control from the debugger context) and
  918. then retry the file operation:
  919.  
  920. Lisp> (open "FOO" :direction :output)
  921. Error: The directory PS:<JDOE> is full.
  922. To continue, type :CONTINUE followed by an option number:
  923.  1: Try this file operation again.
  924.  2: Expunge PS:<JDOE>.
  925.  3: Return to Lisp Toplevel.
  926. Debug> :continue 2
  927. Expunging PS:<JDOE> ... 3 records freed.
  928. Debug> :continue 1
  929.  => #<OUTPUT-STREAM "PS:<JDOE>FOO.LSP" 2323473>
  930.  
  931. [change_end]
  932.  
  933. -------------------------------------------------------------------------------
  934.  
  935. 29.3.11. Interactive Condition Handling
  936.  
  937. [change_begin]
  938. When a program does not know how to continue, and no active handler is able to
  939. advise it, the ``interactive condition handler,'' or ``debugger,'' can be
  940. entered. This happens implicitly through the use of functions such as error and
  941. cerror, or explicitly through the use of the function invoke-debugger.
  942.  
  943. The interactive condition handler never returns directly; it returns only
  944. through structured non-local transfer of control to specially defined restart
  945. points that can be set up either by the system or by user code. The mechanisms
  946. that support the establishment of such structured restart points for portable
  947. code are outlined in sections 29.3.5 through 29.3.10.
  948.  
  949. Actually, implementations may also provide extended debugging facilities that
  950. allow return from arbitrary stack frames. Although such commands are frequently
  951. useful in practice, their effects are implementation-dependent because they
  952. violate the Common Lisp program abstraction. The effect of using such commands
  953. is undefined with respect to Common Lisp.
  954. [change_end]
  955.  
  956. -------------------------------------------------------------------------------
  957.  
  958. 29.3.12. Serious Conditions
  959.  
  960. [change_begin]
  961. The ignore-errors macro will trap conditions of type error. There are, however,
  962. conditions that are not of type error.
  963.  
  964. Some conditions are not considered errors but are still very serious, so we
  965. call them serious conditions and we use the type serious-condition to represent
  966. them. Conditions such as those that might be signaled for ``stack overflow'' or
  967. ``storage exhausted'' are in this category.
  968.  
  969. The type error is a subtype of serious-condition, and it would technically be
  970. correct to use the term ``serious condition'' to refer to all serious
  971. conditions whether errors or not. However, normally we use the term ``serious
  972. condition'' to refer to things of type serious-condition but not of type error.
  973.  
  974. The point of the distinction between errors and other serious conditions is
  975. that some conditions are known to occur for reasons that are beyond the scope
  976. of Common Lisp to specify clearly. For example, we know that a stack will
  977. generally be used to implement function calling, and we know that stacks tend
  978. to be of finite size and are prone to overflow. Since the available stack size
  979. may vary from implementation to implementation, from session to session, or
  980. from function call to function call, it would be confusing to have expressions
  981. such as (ignore-errors (+ a b)) return a number sometimes and nil other times
  982. if a and b were always bound to numbers and the stack just happened to overflow
  983. on a particular call. For this reason, only conditions of type error and not
  984. all conditions of type serious-condition are trapped by ignore-errors. To trap
  985. other conditions, a lower-level facility must be used (such as handler-bind or
  986. handler-case).
  987.  
  988. By convention, the function error is preferred over signal to signal conditions
  989. of type serious-condition (including those of type error). It is the use of the
  990. function error, and not the type of the condition being signaled, that actually
  991. causes the debugger to be entered.
  992.  
  993. -------------------------------------------------------------------------------
  994. Compatibility note: The Common Lisp Condition System differs from that of
  995. Zetalisp in this respect. In Zetalisp the debugger is entered for an unhandled
  996. signal if the error function is used or if the condition is of type error.
  997. -------------------------------------------------------------------------------
  998.  
  999. [change_end]
  1000.  
  1001. -------------------------------------------------------------------------------
  1002.  
  1003. 29.3.13. Non-Serious Conditions
  1004.  
  1005. [change_begin]
  1006. Some conditions are neither errors nor serious conditions. They are signaled to
  1007. give other programs a chance to intervene, but if no action is taken,
  1008. computation simply continues normally.
  1009.  
  1010. For example, an implementation might choose to signal a non-serious (and
  1011. implementation-dependent) condition called end-of-line when output reaches the
  1012. last character position on a line of character output. In such an
  1013. implementation, the signaling of this condition might allow a convenient way
  1014. for other programs to intervene, producing output that is truncated at the end
  1015. of a line.
  1016.  
  1017. By convention, the function signal is used to signal conditions that are not
  1018. serious. It would be possible to signal serious conditions using signal, and
  1019. the debugger would not be entered if the condition went unhandled. However, by
  1020. convention, handlers will generally tend to assume that serious conditions and
  1021. errors were signaled by calling the error function (and will therefore force
  1022. entry to the interactive condition handler) and that they should work to avoid
  1023. this.
  1024. [change_end]
  1025.  
  1026. -------------------------------------------------------------------------------
  1027.  
  1028. 29.3.14. Condition Types
  1029.  
  1030. [change_begin]
  1031. Some types of conditions are predefined by the system. All types of conditions
  1032. are subtypes of condition. That is, (typep x 'condition) is true if and only if
  1033. the value of x is a condition.
  1034.  
  1035. Implementations supporting multiple (or non-hierarchical) type inheritance are
  1036. expressly permitted to exploit multiple inheritance in the tree of condition
  1037. types as implementation-dependent extensions, as long as such extensions are
  1038. compatible with the specifications in this chapter. [X3J13 voted in March 1989
  1039. (ZLOS-CONDITIONS)   to integrate the Condition System and the Object System, so
  1040. multiple inheritance is always available for condition types.-GLS]
  1041.  
  1042. In order to avoid problems in portable code that runs both in systems with
  1043. multiple type inheritance and in systems without it, programmers are explicitly
  1044. warned that while all correct Common Lisp implementations will ensure that
  1045. (typep c 'condition) is true for all conditions c (and all subtype
  1046. relationships indicated in this chapter will also be true), it should not be
  1047. assumed that two condition types specified to be subtypes of the same third
  1048. type are disjoint. (In some cases, disjoint subtypes are identified explicitly,
  1049. but such disjointness is not to be assumed by default.) For example, it follows
  1050. from the subtype descriptions contained in this chapter that in all
  1051. implementations (typep c 'control-error) implies (typep c 'error), but note
  1052. that (typep c 'control-error) does not imply (not (typep c 'cell-error)).
  1053. [change_end]
  1054.  
  1055. -------------------------------------------------------------------------------
  1056.  
  1057. 29.3.15. Signaling Conditions
  1058.  
  1059. [change_begin]
  1060. When a condition is signaled, the system tries to locate the most appropriate
  1061. handler for the condition and to invoke that handler.
  1062.  
  1063. Handlers are established dynamically using handler-bind or abstractions built
  1064. on handler-bind.
  1065.  
  1066. If an appropriate handler is found, it is called. In some circumstances, the
  1067. handler may decline simply by returning without performing a non-local transfer
  1068. of control. In such cases, the search for an appropriate handler is picked up
  1069. where it left off, as if the called handler had never been present.
  1070.  
  1071. If no handler is found, or if all handlers that were found decline, signal
  1072. returns nil.
  1073.  
  1074. Although it follows from the description above, it is perhaps worth noting
  1075. explicitly that the lookup procedure described here will prefer a general but
  1076. more (dynamically) local handler over a specific but less (dynamically) local
  1077. handler. Experience with existing condition systems suggests that this is a
  1078. reasonable approach and works adequately in most situations. Some care should
  1079. be taken when binding handlers for very general kinds of conditions, such as is
  1080. done in ignore-errors. Often, binding for a more specific condition type than
  1081. error is more appropriate.
  1082. [change_end]
  1083.  
  1084. -------------------------------------------------------------------------------
  1085.  
  1086. 29.3.16. Resignaling Conditions
  1087.  
  1088. [change_begin]
  1089. [The contents of this section are still a subject of some debate within X3J13.
  1090. The reader may wish to take this section with a grain of salt.-GLS]
  1091.  
  1092. Note that signaling a condition has no side effect on that condition, and that
  1093. there is no dynamic state contained in a condition object. As such, it may at
  1094. times be reasonable and appropriate to consider caching condition objects for
  1095. repeated use, re-signaling conditions from within handlers, or saving
  1096. conditions away somewhere and re-signaling them later.
  1097.  
  1098. For example, it may be desirable for the system to pre-allocate objects of type
  1099. storage-condition so that they can be signaled when needed without attempting
  1100. to allocate more storage.
  1101. [change_end]
  1102.  
  1103. -------------------------------------------------------------------------------
  1104.  
  1105. 29.3.17. Condition Handlers
  1106.  
  1107. [change_begin]
  1108. A handler is a function of one argument, the condition to be handled. The
  1109. handler may inspect the object to be sure it is ``interested'' in handling the
  1110. condition.
  1111.  
  1112. A handler is executed in the dynamic context of the signaler, except that the
  1113. set of available condition handlers will have been rebound to the value that
  1114. was active at the time the condition handler was made active. The intent of
  1115. this is to prevent infinite recursion because of errors in a condition handler.
  1116.  
  1117. After inspecting the condition, the handler should take one of the following
  1118. actions:
  1119.  
  1120.    *  It might decline to handle the condition (by simply returning). When this
  1121.      happens, the returned values are ignored and the effect is the same as if
  1122.      the handler had been invisible to the mechanism seeking to find a handler.
  1123.      The next handler in line will be tried, or if no such handler exists, the
  1124.      condition will go unhandled.
  1125.  
  1126.    *  It might handle the condition (by performing some non-local transfer of
  1127.      control). This may be done either primitively using go, return, or throw,
  1128.      or more abstractly using a function such as abort or invoke-restart.
  1129.  
  1130.    *  It might signal another condition.
  1131.  
  1132.    *  It might invoke the interactive debugger.
  1133.  
  1134. In fact, the latter two actions (signaling another condition or entering the
  1135. debugger) are really just ways of putting off the decision to either handle or
  1136. decline, or trying to get someone else to make such a decision. Ultimately, all
  1137. a handler can do is to handle or decline to handle.
  1138. [change_end]
  1139.  
  1140. -------------------------------------------------------------------------------
  1141.  
  1142. 29.3.18. Printing Conditions
  1143.  
  1144. [change_begin]
  1145. When *print-escape* is nil (for example, when the princ function or the ~A
  1146. directive is used with format), the report method for the condition will be
  1147. invoked. This will be done automatically by functions such as invoke-debugger,
  1148. break, and warn, but there may still be situations in which it is desirable to
  1149. have a condition report under explicit user control. For example,
  1150.  
  1151. (let ((form '(open "nosuchfile")))
  1152.   (handler-case (eval form)
  1153.     (serious-condition (c)
  1154.       (format t "~&Evaluation of ~S failed:~%~A" form c))))
  1155.  
  1156. might print something like
  1157.  
  1158. Evaluation of (OPEN "nosuchfile") failed:
  1159. The file "nosuchfile" was not found.
  1160.  
  1161. Some suggestions about the form of text typed by report methods:
  1162.  
  1163.    *  The message should generally be a complete sentence, beginning with a
  1164.      capital letter and ending with appropriate punctuation (usually a period).
  1165.  
  1166.    *  The message should not include any introductory text such as ``Error:''
  1167.      or ``Warning:'' and should not be followed by a trailing newline. Such
  1168.      text will be added as may be appropriate to context by the routine
  1169.      invoking the report method.
  1170.  
  1171.    *  Except where unavoidable, the tab character (which is only semi-standard
  1172.      anyway) should not be used in error messages. Its effect may vary from one
  1173.      implementation to another and may cause problems even within an
  1174.      implementation because it may do different things depending on the column
  1175.      at which the error report begins.
  1176.  
  1177.    *  Single-line messages are preferred, but newlines in the middle of long
  1178.      messages are acceptable.
  1179.  
  1180.    *  If any program (for example, the debugger) displays messages indented
  1181.      from the prevailing left margin (for example, indented seven spaces
  1182.      because they are prefixed by the seven-character herald ``Error: ''), then
  1183.      that program will take care of inserting the appropriate indentation into
  1184.      the extra lines of a multi-line error message. Similarly, a program that
  1185.      prefixes error messages with semicolons so that they appear to be comments
  1186.      should take care of inserting a semicolon at the beginning of each line in
  1187.      a multi-line error message. (These rules are important because, even
  1188.      within a single implementation, there may be more than one program that
  1189.      presents error messages to the user, and they may use different styles of
  1190.      presentation. The caller of error cannot anticipate all such possible
  1191.      styles, and so it is incumbent upon the presenter of the message to make
  1192.      any necessary adjustments.)
  1193.  
  1194. [Note: These recommendations expand upon those in section 24.1.-GLS]
  1195.  
  1196. When *print-escape* is not nil, the object should print in some useful (but
  1197. usually fairly abbreviated) fashion according to the style of the
  1198. implementation. It is not expected that a condition will be printed in a form
  1199. suitable for read. Something like #<ARITHMETIC-ERROR 1734> is fine.
  1200.  
  1201. X3J13 voted in March 1989 (ZLOS-CONDITIONS)   to integrate the Condition System
  1202. and the Object System. In the original Condition System proposal, no function
  1203. was provided for directly accessing or setting the printer for a condition
  1204. type, or for invoking it; the techniques described above were the sole
  1205. interface to reporting. The vote specified that, in CLOS terms, condition
  1206. reporting is mediated through the print-object method for the condition type
  1207. (that is, class) in question, with *print-escape* bound to nil. Specifying
  1208. (:report fn) to define-condition when defining condition type C is equivalent
  1209. to a separate method definition:
  1210.  
  1211. (defmethod print-object ((x C) stream)
  1212.   (if *print-escape*
  1213.       (call-next-method)
  1214.       (funcall #'fn x stream)))
  1215.  
  1216. Note that the method uses fn to print the condition only when *print-escape*
  1217. has the value nil.
  1218. [change_end]
  1219.  
  1220. -------------------------------------------------------------------------------
  1221.  
  1222. 29.4. Program Interface to the Condition System
  1223.  
  1224. [change_begin]
  1225. This section describes functions, macros, variables, and condition types
  1226. associated with the Common Lisp Condition System.
  1227. [change_end]
  1228.  
  1229. -------------------------------------------------------------------------------
  1230.  
  1231.    *  Signaling Conditions
  1232.    *  Assertions
  1233.    *  Exhaustive Case Analysis
  1234.    *  Handling Conditions
  1235.    *  Defining Conditions
  1236.    *  Creating Conditions
  1237.    *  Establishing Restarts
  1238.    *  Finding and Manipulating Restarts
  1239.    *  Warnings
  1240.    *  Restart Functions
  1241.    *  Debugging Utilities
  1242.  
  1243. -------------------------------------------------------------------------------
  1244.  
  1245. 29.4.1. Signaling Conditions
  1246.  
  1247. [change_begin]
  1248. The functions in this section provide various mechanisms for signaling
  1249. warnings, breaks, continuable errors, and fatal errors.
  1250.  
  1251. [Function]
  1252. error datum &rest arguments
  1253.  
  1254. [This supersedes the description of error given in section 24.1.-GLS]
  1255.  
  1256. Invokes the signal facility on a condition. If the condition is not handled,
  1257. (invoke-debugger condition) is executed. As a consequence of calling
  1258. invoke-debugger, error never directly returns to its caller; the only exit from
  1259. this function can come by non-local transfer of control in a handler or by use
  1260. of an interactive debugging command.
  1261.  
  1262. If datum is a condition, then that condition is used directly. In this case, it
  1263. is an error for the list of arguments to be non-empty; that is, error must have
  1264. been called with exactly one argument, the condition.
  1265.  
  1266. If datum is a condition type (a class or class name), then the condition used
  1267. is effectively the result of (apply #'make-condition datum arguments).
  1268.  
  1269. If datum is a string, then the condition used is effectively the result of
  1270.  
  1271. (make-condition 'simple-error
  1272.                 :format-string datum
  1273.                 :format-arguments arguments)
  1274.  
  1275. [Function]
  1276. cerror continue-format-string datum &rest arguments
  1277.  
  1278. [This supersedes the description of cerror given in section 24.1.-GLS]
  1279.  
  1280. The function cerror invokes the error facility on a condition. If the condition
  1281. is not handled, (invoke-debugger condition) is executed. While signaling is
  1282. going on, and while control is in the debugger (if it is reached), it is
  1283. possible to continue program execution (thereby returning from the call to
  1284. cerror) using the continue restart.
  1285.  
  1286. If datum is a condition, then that condition is used directly. In this case,
  1287. the list of arguments need not be empty, but will be used only with the
  1288. continue-format-string and will not be used to initialize datum.
  1289.  
  1290. If datum is a condition type (a class or class name), then the condition used
  1291. is effectively the result of (apply #'make-condition datum arguments).
  1292.  
  1293. If datum is a string, then the condition used is effectively the result of
  1294.  
  1295. (make-condition 'simple-error
  1296.                 :format-string datum
  1297.                 :format-arguments arguments)
  1298.  
  1299. The continue-format-string must be a string. Note that if datum is not a
  1300. string, then the format arguments used by the continue-format-string will still
  1301. be the list of arguments (which is in keyword format if datum is a condition
  1302. type). In this case, some care may be necessary to set up the
  1303. continue-format-string correctly. The format directive ~*, which ignores and
  1304. skips over format arguments, may be particularly useful in this situation.
  1305.  
  1306. The value returned by cerror is nil.
  1307.  
  1308. [Function]
  1309. signal datum &rest arguments
  1310.  
  1311. Invokes the signal facility on a condition. If the condition is not handled,
  1312. signal returns nil.
  1313.  
  1314. If datum is a condition, then that condition is used directly. In this case, it
  1315. is an error for the list of arguments to be non-empty; that is, error must have
  1316. been called with exactly one argument, the condition.
  1317.  
  1318. If datum is a condition type (a class or class name), then the condition used
  1319. is effectively the result of (apply #'make-condition datum arguments).
  1320.  
  1321. If datum is a string, then the condition used is effectively the result of
  1322.  
  1323. (make-condition 'simple-error
  1324.                 :format-string datum
  1325.                 :format-arguments arguments)
  1326.  
  1327. Note that if (typep condition *break-on-signals*) is true, then the debugger
  1328. will be entered prior to beginning the process of signaling. The continue
  1329. restart function may be used to continue with the signaling process; the
  1330. restart is associated with the signaled condition as if by use of
  1331. with-condition-restarts. This is true also for all other functions and macros
  1332. that signal conditions, such as warn, error, cerror, assert, and check-type.
  1333.  
  1334. During the dynamic extent of a call to signal with a particular condition, the
  1335. effect of calling signal again on that condition object for a distinct abstract
  1336. event is not defined. For example, although a handler may resignal a condition
  1337. in order to allow outer handlers first shot at handling the condition, two
  1338. distinct asynchronous keyboard events must not signal an the same (eq)
  1339. condition object at the same time.
  1340.  
  1341. For further details about signaling and handling, see the discussion of
  1342. condition handlers in section 29.3.17.
  1343.  
  1344. [Variable]
  1345. *break-on-signals*
  1346.  
  1347. This variable is intended primarily for use when the user is debugging programs
  1348. that do signaling. The value of *break-on-signals* should be suitable as a
  1349. second argument to typep, that is, a type or type specifier.
  1350.  
  1351. When (typep condition *break-on-signals*) is true, then calls to signal (and to
  1352. other advertised functions such as error that implicitly call signal) will
  1353. enter the debugger prior to signaling that condition. The continue restart may
  1354. be used to continue with the normal signaling process; the restart is
  1355. associated with the signaled condition as if by use of with-condition-restarts.
  1356.  
  1357. Note that nil is a valid type specifier. If the value of *break-on-signals* is
  1358. nil, then signal will never enter the debugger in this implicit manner.
  1359.  
  1360. When setting this variable, the user is encouraged to choose the most
  1361. restrictive specification that suffices. Setting this flag effectively violates
  1362. the modular handling of condition signaling that this chapter seeks to
  1363. establish. Its complete effect may be unpredictable in some cases, since the
  1364. user may not be aware of the variety or number of calls to signal that are used
  1365. in programs called only incidentally.
  1366.  
  1367. By default-and certainly in any ``production'' use-the value of this variable
  1368. should be nil, both for reasons of performance and for reasons of modularity
  1369. and abstraction.
  1370.  
  1371. X3J13 voted in March 1989 (BREAK-ON-WARNINGS-OBSOLETE)   to remove
  1372. *break-on-warnings* from the language; *break-on-signals* offers all the power
  1373. of *break-on-warnings* and more.
  1374.  
  1375. -------------------------------------------------------------------------------
  1376. Compatibility note: This variable is similar to the Zetalisp variable
  1377. trace-conditions except for the obvious difference that zl:trace-conditions
  1378. takes a type or list of types while *break-on-signals* takes a single type
  1379. specifier.
  1380.  
  1381. [There is no loss of generality in Common Lisp because the or type specifier
  1382. may be used to indicate that any of a set of conditions should enter the
  1383. debugger.-GLS]
  1384. -------------------------------------------------------------------------------
  1385.  
  1386. [change_end]
  1387.  
  1388. -------------------------------------------------------------------------------
  1389.  
  1390. 29.4.2. Assertions
  1391.  
  1392. [change_begin]
  1393. These facilities are designed to make it convenient for the user to insert
  1394. error checks into code.
  1395.  
  1396. [Macro]
  1397. check-type place typespec [string]
  1398.  
  1399. [This supersedes the description of check-type given in section 24.2.-GLS]
  1400.  
  1401. A check-type form signals an error of type type-error if the contents of place
  1402. are not of the desired type.
  1403.  
  1404. If a condition is signaled, handlers of this condition can use the functions
  1405. type-error-datum and type-error-expected-type to access the contents of place
  1406. and the typespec, respectively.
  1407.  
  1408. This function can return only if the store-value restart is invoked, either
  1409. explicitly from a handler or implicitly as one of the options offered by the
  1410. debugger. The restart is associated with the signaled condition as if by use of
  1411. with-condition-restarts.
  1412.  
  1413. If store-value is called, check-type will store the new value that is the
  1414. argument to store-value (or that is prompted for interactively by the debugger)
  1415. in place and start over, checking the type of the new value and signaling
  1416. another error if it is still not the desired type. Subforms of place may be
  1417. evaluated multiple times because of the implicit loop generated. check-type
  1418. returns nil.
  1419.  
  1420. The place must be a generalized variable reference acceptable to setf. The
  1421. typespec must be a type specifier; it is not evaluated. The string should be an
  1422. English description of the type, starting with an indefinite article (``a'' or
  1423. ``an''); it is evaluated. If the string is not supplied, it is computed
  1424. automatically from the typespec. (The optional string argument is allowed
  1425. because some applications of check-type may require a more specific description
  1426. of what is wanted than can be generated automatically from the type specifier.)
  1427.  
  1428. The error message will mention the place, its contents, and the desired type.
  1429.  
  1430. -------------------------------------------------------------------------------
  1431. Implementation note: An implementation may choose to generate a somewhat
  1432. differently worded error message if it recognizes that place is of a particular
  1433. form, such as one of the arguments to the function that called check-type.
  1434. -------------------------------------------------------------------------------
  1435.  
  1436. Lisp> (setq aardvarks '(sam harry fred))
  1437.  => (SAM HARRY FRED)
  1438. Lisp> (check-type aardvarks (array * (3)))
  1439. Error: The value of AARDVARKS, (SAM HARRY FRED),
  1440.        is not a 3-long array.
  1441. To continue, type :CONTINUE followed by an option number:
  1442.  1: Specify a value to use instead.
  1443.  2: Return to Lisp Toplevel.
  1444. Debug> :continue 1
  1445. Use Value: #(sam fred harry)
  1446.  => NIL
  1447. Lisp> aardvarks
  1448.  => #<ARRAY-3 13571>
  1449. Lisp> (map 'list #'identity aardvarks)
  1450.  => (SAM FRED HARRY)
  1451. Lisp> (setq aacount 'foo)
  1452.  => FOO
  1453. Lisp> (check-type aacount (integer 0 *) "a non-negative integer")
  1454. Error: The value of AACOUNT, FOO, is not a non-negative integer.
  1455. To continue, type :CONTINUE followed by an option number:
  1456.  1: Specify a value to use instead.
  1457.  2: Return to Lisp Toplevel.
  1458. Debug> :continue 2
  1459. Lisp>
  1460.  
  1461. -------------------------------------------------------------------------------
  1462. Compatibility note: In Zetalisp, the equivalent facility is called
  1463. check-arg-type.
  1464. -------------------------------------------------------------------------------
  1465.  
  1466. [Macro]
  1467. assert test-form [({place}*) [datum {argument}*]]
  1468.  
  1469. [This supersedes the description of assert given in section 24.2.-GLS]
  1470.  
  1471. An assert form signals an error if the value of the test-form is nil.
  1472. Continuing from this error using the continue restart will allow the user to
  1473. alter the values of some variables, and assert will then start over, evaluating
  1474. the test-form again. (The restart is associated with the signaled condition as
  1475. if by use of with-condition-restarts.) assert returns nil.
  1476.  
  1477. The test-form may be any form. Each place (there may be any number of them, or
  1478. none) must be a generalized variable reference acceptable to setf. These should
  1479. be variables on which test-form depends, whose values may sensibly be changed
  1480. by the user in attempting to correct the error. Subforms of each place are
  1481. evaluated only if an error is signaled, and may be re-evaluated if the error is
  1482. re-signaled (after continuing without actually fixing the problem).
  1483.  
  1484. The datum and arguments are evaluated only if an error is to be signaled, and
  1485. re-evaluated if the error is to be signaled again.
  1486.  
  1487. If datum is a condition, then that condition is used directly. In this case, it
  1488. is an error to specify any arguments.
  1489.  
  1490. If datum is a condition type (a class or class name), then the condition used
  1491. is effectively the result of (apply #'make-condition datum (list argument)).
  1492.  
  1493. If datum is a string, then the condition used is effectively the result of
  1494.  
  1495. (make-condition 'simple-error
  1496.                 :format-string datum
  1497.                 :format-arguments (list argument))
  1498.  
  1499. If datum is omitted, then a condition of type simple-error is constructed using
  1500. the test-form as data. For example, the following might be used:
  1501.  
  1502. (make-condition 'simple-error
  1503.   :format-string "The assertion ~S failed."
  1504.   :format-arguments '(test-form))
  1505.  
  1506. Note that the test-form itself, and not its value, is used as the format
  1507. argument.
  1508.  
  1509. -------------------------------------------------------------------------------
  1510. Implementation note: The debugger need not include the test-form in the error
  1511. message, and any places should not be included in the message, but they should
  1512. be made available for the user's perusal. If the user gives the ``continue''
  1513. command, an opportunity should be presented to alter the values of any or all
  1514. of the references. The details of this depend on the implementation's style of
  1515. user interface, of course.
  1516. -------------------------------------------------------------------------------
  1517.  
  1518. Here is an example of the use of assert:
  1519.  
  1520. (setq x (make-array '(3 5) :initial-element 3))
  1521. (setq y (make-array '(3 5) :initial-element 7))
  1522.  
  1523. (defun matrix-multiply (a b)
  1524.   (let ((*print-array* nil))
  1525.     (assert (and (= (array-rank a) (array-rank b) 2)
  1526.                  (= (array-dimension a 1)
  1527.                     (array-dimension b 0)))
  1528.             (a b)
  1529.             "Cannot multiply ~S by ~S." a b)
  1530.     (really-matrix-multiply a b)))
  1531.  
  1532. (matrix-multiply x y)
  1533. Error: Cannot multiply #<ARRAY-3-5 12345> by #<ARRAY-3-5 12364>.
  1534. To continue, type :CONTINUE followed by an option number:
  1535.  1: Specify new values.
  1536.  2: Return to Lisp Toplevel.
  1537. Debug> :continue 1
  1538. Value for A: x
  1539. Value for B: (make-array '(5 3) :initial-element 6)
  1540.  => #2A(Ø(54 54 54 54 54)
  1541. (54 54 54 54 54)
  1542.                 (54 54 54 54 54)
  1543.                 (54 54 54 54 54)
  1544.                 (54 54 54 54 54))
  1545.  
  1546. [change_end]
  1547.  
  1548. -------------------------------------------------------------------------------
  1549.  
  1550. 29.4.3. Exhaustive Case Analysis
  1551.  
  1552. [change_begin]
  1553. The syntax for etypecase and ctypecase is the same as for typecase, except that
  1554. no otherwise clause is permitted. Similarly, the syntax for ecase and ccase is
  1555. the same as for case except for the otherwise clause.
  1556.  
  1557. etypecase and ecase are similar to typecase and case, respectively, but signal
  1558. a non-continuable error rather than returning nil if no clause is selected.
  1559.  
  1560. ctypecase and ccase are also similar to typecase and case, respectively, but
  1561. signal a continuable error if no clause is selected.
  1562.  
  1563. [Macro]
  1564. etypecase keyform {(type {form}*)}*
  1565.  
  1566. [This supersedes the description of etypecase given in section 24.3.-GLS]
  1567.  
  1568. This control construct is similar to typecase, but no explicit otherwise or t
  1569. clause is permitted. If no clause is satisfied, etypecase signals an error (of
  1570. type type-error) with a message constructed from the clauses. It is not
  1571. permissible to continue from this error. To supply an error message, the user
  1572. should use typecase with an otherwise clause containing a call to error. The
  1573. name of this function stands for ``exhaustive type case'' or ``error-checking
  1574. type case.''
  1575.  
  1576. Example:
  1577.  
  1578. Lisp> (setq x 1/3)
  1579.  => 1/3
  1580. Lisp> (etypecase x
  1581.         (integer (* x 4))
  1582.         (symbol (symbol-value x)))
  1583. Error: The value of X, 1/3, is neither an integer nor a symbol.
  1584. To continue, type :CONTINUE followed by an option number:
  1585.  1: Return to Lisp Toplevel.
  1586. Debug>
  1587.  
  1588. [Macro]
  1589. ctypecase keyplace {(type {form}*)}*
  1590.  
  1591. [This supersedes the description of ctypecase given in section 24.3.-GLS]
  1592.  
  1593. This control construct is similar to typecase, but no explicit otherwise or t
  1594. clause is permitted.
  1595.  
  1596. The keyplace must be a generalized variable reference acceptable to setf. If no
  1597. clause is satisfied, ctypecase signals an error (of type type-error) with a
  1598. message constructed from the clauses. This error may be continued using the
  1599. store-value restart. The argument to store-value is stored in keyplace and then
  1600. ctypecase starts over, making the type tests again. Subforms of keyplace may be
  1601. evaluated multiple times. If the store-value restart is invoked interactively,
  1602. the user will be prompted for the value to be used.
  1603.  
  1604. The name of this function is mnemonic for ``continuable (exhaustive) type
  1605. case.''
  1606.  
  1607. Example:
  1608.  
  1609. Lisp> (setq x 1/3)
  1610.  => 1/3
  1611. Lisp> (ctypecase x
  1612.         (integer (* x 4))
  1613.         (symbol (symbol-value x)))
  1614. Error: The value of X, 1/3, is neither an integer nor a symbol.
  1615. To continue, type :CONTINUE followed by an option number:
  1616.  1: Specify a value to use instead.
  1617.  2: Return to Lisp Toplevel.
  1618. Debug> :continue 1
  1619. Use value: 3.7
  1620. Error: The value of X, 3.7, is neither an integer nor a symbol.
  1621. To continue, type :CONTINUE followed by an option number:
  1622.  1: Specify a value to use instead.
  1623.  2: Return to Lisp Toplevel.
  1624. Debug> :continue 1
  1625. Use value: 12
  1626.  => 48
  1627.  
  1628. [Macro]
  1629. ecase keyform {({({key}*) | key} {form}*)}*
  1630.  
  1631. [This supersedes the description of ecase given in section 24.3.-GLS]
  1632.  
  1633. This control construct is similar to case, but no explicit otherwise or t
  1634. clause is permitted. If no clause is satisfied, ecase signals an error (of type
  1635. type-error) with a message constructed from the clauses. It is not permissible
  1636. to continue from this error. To supply an error message, the user should use
  1637. case with an otherwise clause containing a call to error. The name of this
  1638. function stands for ``exhaustive case'' or ``error-checking case.''
  1639.  
  1640. Example:
  1641.  
  1642. Lisp> (setq x 1/3)
  1643.  => 1/3
  1644. Lisp> (ecase x
  1645.         (alpha (foo))
  1646.         (omega (bar))
  1647.         ((zeta phi) (baz)))
  1648. Error: The value of X, 1/3, is not ALPHA, OMEGA, ZETA, or PHI.
  1649. To continue, type :CONTINUE followed by an option number:
  1650.  1: Return to Lisp Toplevel.
  1651. Debug>
  1652.  
  1653. [Macro]
  1654. ccase keyplace {({({key}*) | key} {form}*)}*
  1655.  
  1656. [This supersedes the description of ccase given in section 24.3.-GLS]
  1657.  
  1658. This control construct is similar to case, but no explicit otherwise or t
  1659. clause is permitted.
  1660.  
  1661. The keyplace must be a generalized variable reference acceptable to setf. If no
  1662. clause is satisfied, ccase signals an error (of type type-error) with a message
  1663. constructed from the clauses. This error may be continued using the store-value
  1664. restart. The argument to store-value is stored in keyplace and then ccase
  1665. starts over, making the type tests again. Subforms of keyplace may be evaluated
  1666. multiple times. If the store-value restart is invoked interactively, the user
  1667. will be prompted for the value to be used.
  1668.  
  1669. The name of this function is mnemonic for ``continuable (exhaustive) case.''
  1670.  
  1671. -------------------------------------------------------------------------------
  1672. Implementation note: The type-error signaled by ccase and ecase is free to
  1673. choose any representation of the acceptable argument type that it wishes for
  1674. placement in the expected-type slot. It will always work to use type (member .
  1675. keys), but in some cases it may be more efficient, for example, to use a type
  1676. that represents an integer subrange or a type composed using the or type
  1677. specifier.
  1678. -------------------------------------------------------------------------------
  1679.  
  1680. [change_end]
  1681.  
  1682. -------------------------------------------------------------------------------
  1683.  
  1684. 29.4.4. Handling Conditions
  1685.  
  1686. [change_begin]
  1687. These macros allow a program to gain control when a condition is signaled.
  1688.  
  1689. [Macro]
  1690. handler-case expression {(typespec ([var]) {form}*)}*
  1691.  
  1692. Executes the given expression in a context where various specified handlers are
  1693. active.
  1694.  
  1695. Each typespec may be any type specifier. If during the execution of the
  1696. expression a condition is signaled for which there is an appropriate
  1697. clause-that is, one for which (typep condition 'typespec) is true-and if there
  1698. is no intervening handler for conditions of that type, then control is
  1699. transferred to the body of the relevant clause (unwinding the dynamic state
  1700. appropriately in the process) and the given variable var is bound to the
  1701. condition that was signaled. If no such condition is signaled and the
  1702. computation runs to completion, then the values resulting from the expression
  1703. are returned by the handler-case form.
  1704.  
  1705. If more than one case is provided, those cases are made accessible in parallel.
  1706. That is, in
  1707.  
  1708. (handler-case expression
  1709.   (type1 (var1) form1)
  1710.   (type2 (var2) form2))
  1711.  
  1712. if the first clause (containing form1) has been selected, the handler for the
  1713. second is no longer visible (and vice versa).
  1714.  
  1715. The cases are searched sequentially from top to bottom. If a signaled condition
  1716. matches more than one case (possible if there is type overlap) the earlier of
  1717. the two cases will be selected.
  1718.  
  1719. If the variable var is not needed, it may be omitted. That is, a clause such as
  1720.  
  1721. (type (var) (declare (ignore var)) form)
  1722.  
  1723. may be written using the following shorthand notation:
  1724.  
  1725. (type () form)
  1726.  
  1727. If there are no forms in a selected case, the case returns nil. Note that
  1728.  
  1729. (handler-case expression
  1730.   (type1 (var1) . body1)
  1731.   (type2 (var2) . body2)
  1732.   ...)
  1733.  
  1734. is approximately equivalent to
  1735.  
  1736. (block #1=#:block-1
  1737.   (let (#2=#:var-2)
  1738.     (tagbody
  1739.       (handler-bind ((type1 Ø#'(lambda (temp)
  1740.                     (setq #2# temp)
  1741.                     (go #3=#:tag-3)))
  1742.                      (type2 Ø#'(lambda (temp)
  1743.                     (setq #2# temp)
  1744.                     (go #4=#:tag-4)))
  1745.                      ...)
  1746.         (return-from #1# expression))
  1747.       #3# (return-from #1# (let ((var1 #2#)) . body1))
  1748.       #4# (return-from #1# (let ((var2 #2#)) . body2))
  1749.       ...)))
  1750.  
  1751. [Note the use of ``gensyms'' such as #:block-1 as block names, variables, and
  1752. tagbody tags in this example, and the use of #n= and #n# read-macro syntax to
  1753. indicate that the very same gensym appears in multiple places.-GLS]
  1754.  
  1755. As a special case, the typespec can also be the symbol :no-error in the last
  1756. clause. If it is, it designates a clause that will take control if the
  1757. expression returns normally. In that case, a completely general lambda-list may
  1758. follow the symbol :no-error, and the arguments to which the lambda-list
  1759. parameters are bound are like those for multiple-value-call on the return value
  1760. of the expression. For example,
  1761.  
  1762. (handler-case expression
  1763.   (type1 (var1) . body1)
  1764.   (type2 (var2) . body2)
  1765.   ...
  1766.   (typen (varn) . bodyn)
  1767.   (:no-error (nvar1 nvar2 ... nvarm) . nbody))
  1768.  
  1769. is approximately equivalent to
  1770.  
  1771. (block #1=#:error-return
  1772.   (multiple-value-call #'(lambda (nvar1 nvar2 ... nvarm) . nbody)
  1773.     (block #2=#:normal-return
  1774.       (return-from #1#
  1775.         (handler-case (return-from #2# expression)
  1776.           (type1 (var1) . body1)
  1777.           (type2 (var2) . body2)
  1778.           ...
  1779.           (typen (varn) . bodyn))))))
  1780.  
  1781. Examples of the use of handler-case:
  1782.  
  1783. (handler-case (/ x y)
  1784.   (division-by-zero () nil))
  1785.  
  1786. (handler-case (open *the-file* :direction :input)
  1787.   (file-error (condition) (format t "~&Fooey: ~A~%" condition)))
  1788.  
  1789. (handler-case (some-user-function)
  1790.   (file-error (condition) condition)
  1791.   (division-by-zero () 0)
  1792.   ((or unbound-variable undefined-function) () 'unbound))
  1793.  
  1794. (handler-case (intern x y)
  1795.   (error (condition) condition)
  1796.   (:no-error (symbol status)
  1797.     (declare (ignore symbol))
  1798.     status))
  1799.  
  1800. [Macro]
  1801. ignore-errors {form}*
  1802.  
  1803. Executes its body in a context that handles conditions of type error by
  1804. returning control to this form. If no such condition is signaled, any values
  1805. returned by the last form are returned by ignore-errors. Otherwise, two values
  1806. are returned: nil and the error condition that was signaled.
  1807.  
  1808. ignore-errors could be defined by
  1809.  
  1810. (defmacro ignore-errors (&body forms)
  1811.   `(handler-case (progn ,@forms)
  1812.      (error (c) (values nil c)))
  1813.  
  1814. [Macro]
  1815. handler-bind ({(typespec handler)}*) {form}*
  1816.  
  1817. Executes body in a dynamic context where the given handler bindings are in
  1818. effect. Each typespec may be any type specifier. Each handler form should
  1819. evaluate to a function to be used to handle conditions of the given type(s)
  1820. during execution of the forms. This function should take a single argument, the
  1821. condition being signaled.
  1822.  
  1823. If more than one binding is specified, the bindings are searched sequentially
  1824. from top to bottom in search of a match (by visual analogy with typecase). If
  1825. an appropriate typespec is found, the associated handler is run in a context
  1826. where none of the handler bindings are visible (to avoid recursive errors). For
  1827. example, in the case of
  1828.  
  1829. (handler-bind ((unbound-variable #'(lambda ...))
  1830.                (error #'(lambda ...)))
  1831.   ...)
  1832.  
  1833. if an unbound variable error is signaled in the body (and not handled by an
  1834. intervening handler), the first function will be called. If any other kind of
  1835. error is signaled, the second function will be called. In either case, neither
  1836. handler will be active while executing the code in the associated function.
  1837. [change_end]
  1838.  
  1839. -------------------------------------------------------------------------------
  1840.  
  1841. 29.4.5. Defining Conditions
  1842.  
  1843. [change_begin]
  1844. [The contents of this section are still a subject of some debate within X3J13.
  1845. The reader may wish to take this section with a grain of salt, two aspirin
  1846. tablets, and call a hacker in the morning.-GLS]
  1847.  
  1848. [Macro]
  1849.  
  1850. define-condition name ({parent-type}*)
  1851.                  [({slot-specifier}*) {option}*]
  1852.  
  1853. Defines a new condition type called name, which is a subtype of each given
  1854. parent-type. Except as otherwise noted, the arguments are not evaluated.
  1855.  
  1856. Objects of this condition type will have all of the indicated slots, plus any
  1857. additional slots inherited from the parent types (its superclasses). If the
  1858. slots list is omitted, the empty list is assumed.
  1859.  
  1860. A slot must have the form
  1861.  
  1862. slot-specifier ::= slot-name | (slot-name [[?slot-option]])
  1863.  
  1864. For the syntax of a slot-option, see defclass. The slots of a condition object
  1865. are normal CLOS slots. Note that with-slots may be used instead of accessor
  1866. functions to access slots of a condition object.
  1867.  
  1868. make-condition will accept keywords (in the keyword package) with the print
  1869. name of any of the designated slots, and will initialize the corresponding
  1870. slots in conditions it creates.
  1871.  
  1872. Accessors are created according to the same rules as used by defclass.
  1873.  
  1874. The valid options are as follows:
  1875.  
  1876. (:documentation doc-string)
  1877.  
  1878.      The doc-string should be either nil or a string that describes the purpose
  1879.      of the condition type. If this option is omitted, nil is assumed. Calling
  1880.      (documentation 'name 'type) will retrieve this information.
  1881.  
  1882. (:report exp)
  1883.  
  1884.      If exp is not a literal string, it must be a suitable argument to the
  1885.      function special form. The expression (function exp) will be evaluated in
  1886.      the current lexical environment. It should produce a function of two
  1887.      arguments, a condition and a stream, that prints on the stream a
  1888.      description of the condition. This function is called whenever the
  1889.      condition is printed while *print-escape* is nil.
  1890.  
  1891.      If exp is a literal string, it is shorthand for
  1892.  
  1893.      (lambda (c s)
  1894.        (declare (ignore c))
  1895.        (write-string exp s))
  1896.  
  1897.      [That is, a function is provided that will simply write the given string
  1898.      literally to the stream, regardless of the particular condition object
  1899.      supplied.-GLS]
  1900.  
  1901.      The :report option is processed after the new condition type has been
  1902.      defined, so use of the slot accessors within the report function is
  1903.      permitted. If this option is not specified, information about how to
  1904.      report this type of condition will be inherited from the parent-type.
  1905.  
  1906. [X3J13 voted in March 1989 (ZLOS-CONDITIONS)   to integrate the Condition
  1907. System and the Object System. In the original Condition System proposal,
  1908. define-condition allowed only one parent-type (the inheritance structure was a
  1909. simple hierarchy). Slot descriptions were much simpler, even simpler than those
  1910. for defstruct:
  1911.  
  1912. slot ::= slot-name | (slot-name) | (slot-name default-value)
  1913.  
  1914. Similarly, define-condition allowed a :conc-name option similar to that of
  1915. defstruct:
  1916.  
  1917. (:conc-name symbol-or-string)
  1918.  
  1919.      Not now part of Common Lisp. As with defstruct, this sets up automatic
  1920.      prefixing of the names of slot accessors. Also as in defstruct, the
  1921.      default behavior is to use the name of the new type, name, followed by a
  1922.      hyphen. (Generated names are interned in the package that is current at
  1923.      the time that the define-condition is processed).
  1924.  
  1925. One consequence of the vote was to make define-condition slot descriptions like
  1926. those of defclass.-GLS]
  1927.  
  1928. Here are some examples of the use of define-condition.
  1929.  
  1930. The following form defines a condition of type peg/hole-mismatch that inherits
  1931. from a condition type called blocks-world-error:
  1932.  
  1933. (define-condition peg/hole-mismatch (blocks-world-error)
  1934.                   (peg-shape hole-shape)
  1935.   (:report
  1936.     (lambda (condition stream)
  1937.       (with-slots (peg-shape hole-shape) condition
  1938.         (format stream "A ~A peg cannot go in a ~A hole."
  1939.                 peg-shape hole-shape))))
  1940.  
  1941. The new type has slots peg-shape and hole-shape, so make-condition will accept
  1942. :peg-shape and :hole-shape keywords. The with-slots macro may be used to access
  1943. the peg-shape and hole-shape slots, as illustrated in the :report information.
  1944.  
  1945. Here is another example. This defines a condition called machine-error that
  1946. inherits from error:
  1947.  
  1948. (define-condition machine-error (error)
  1949.                   ((machine-name
  1950.                     :reader machine-error-machine-name))
  1951.   (:report (lambda (condition stream)
  1952.              (format stream "There is a problem with ~A."
  1953.                      (machine-error-machine-name condition)))))
  1954.  
  1955. Building on this definition, we can define a new error condition that is a
  1956. subtype of machine-error for use when machines are not available:
  1957.  
  1958. (define-condition machine-not-available-error (machine-error) ()
  1959.   (:report (lambda (condition stream)
  1960.              (format stream "The machine ~A is not available."
  1961.                      (machine-error-machine-name condition)))))
  1962.  
  1963. We may now define a still more specific condition, built upon
  1964. machine-not-available-error, that provides a default for machine-name but does
  1965. not provide any new slots or report information. It just gives the machine-name
  1966. slot a default initialization:
  1967.  
  1968. (define-condition my-favorite-machine-not-available-error
  1969.                   (machine-not-available-error)
  1970.                   ((machine-name :initform "MC.LCS.MIT.EDU")))
  1971.  
  1972. Note that since no :report clause was given, the information inherited from
  1973. machine-not-available-error will be used to report this type of condition.
  1974. [change_end]
  1975.  
  1976. -------------------------------------------------------------------------------
  1977.  
  1978. 29.4.6. Creating Conditions
  1979.  
  1980. [change_begin]
  1981. The function make-condition is the basic means for creating condition objects.
  1982.  
  1983. [Function]
  1984. make-condition type &rest slot-initializations
  1985.  
  1986. Constructs a condition object of the given type using slot-initializations as a
  1987. specification of the initial value of the slots. The newly created condition is
  1988. returned.
  1989.  
  1990. The slot-initializations are alternating keyword/value pairs. For example:
  1991.  
  1992. (make-condition 'peg/hole-mismatch
  1993.                 :peg-shape 'square :hole-shape 'round)
  1994.  
  1995. [change_end]
  1996.  
  1997. -------------------------------------------------------------------------------
  1998.  
  1999. 29.4.7. Establishing Restarts
  2000.  
  2001. [change_begin]
  2002. The lowest-level form that creates restart points is called restart-bind. The
  2003. restart-case macro is an abstraction that addresses many common needs for
  2004. restart-bind while offering a more palatable syntax. See also
  2005. with-simple-restart. The function that transfers control to a restart point
  2006. established by one of these macros is called invoke-restart.
  2007.  
  2008. All restarts have dynamic extent; a restart does not survive execution of the
  2009. form that establishes it.
  2010.  
  2011. [Macro]
  2012.  
  2013. with-simple-restart (name format-string {format-argument}*)
  2014.            {form}*
  2015.  
  2016. This is shorthand for one of the most common uses of restart-case.
  2017.  
  2018. If the restart designated by name is not invoked while executing the forms, all
  2019. values returned by the last form are returned. If that restart is invoked,
  2020. control is transferred to the with-simple-restart form, which immediately
  2021. returns the two values nil and t.
  2022.  
  2023. The name may be nil, in which case an anonymous restart is established.
  2024.  
  2025. with-simple-restart could be defined by
  2026.  
  2027. (defmacro with-simple-restart ((restart-name format-string
  2028.                                 &rest format-arguments)
  2029.                                &body forms)
  2030.   `(restart-case (progn ,@forms)
  2031.      (,restart-name ()
  2032.        :report
  2033.          (lambda (stream)
  2034.            (format stream ,format-string ,@format-arguments))
  2035.        (values nil t))))
  2036.  
  2037. Here is an example of the use of with-simple-restart.
  2038.  
  2039. Lisp> (defun read-eval-print-loop (level)
  2040.         (with-simple-restart
  2041.             (abort "Exit command level ~D." level)
  2042.           (loop
  2043.             (with-simple-restart
  2044.                 (abort "Return to command level ~D." level)
  2045.               (let ((form (prog2 (fresh-line)
  2046.                                  (read)
  2047.                                  (fresh-line))))
  2048.                 (prin1 (eval form)))))))
  2049.  => READ-EVAL-PRINT-LOOP
  2050. Lisp> (read-eval-print-loop 1)
  2051. (+ 'a 3)
  2052. Error: The argument, A, to the function + was of the wrong type.
  2053.        The function expected a number.
  2054. To continue, type :CONTINUE followed by an option number:
  2055.  1: Specify a value to use this time.
  2056.  2: Return to command level 1.
  2057.  3: Exit command level 1.
  2058.  4: Return to Lisp Toplevel.
  2059. Debug>
  2060.  
  2061. -------------------------------------------------------------------------------
  2062. Compatibility note: In contrast to the way that Zetalisp has traditionally
  2063. defined abort as a kind of condition to be handled, the Common Lisp Condition
  2064. System defines abort as a way to restart (``proceed'' in Zetalisp terms).
  2065. -------------------------------------------------------------------------------
  2066. Remark: Some readers may wonder what ought to be done by the ``abort'' key (or
  2067. whatever the implementation's interrupt key is-Control-C or Control-G, for
  2068. example). Such interrupts, whether synchronous or asynchronous in nature, are
  2069. beyond the scope of this chapter and indeed are not currently addressed by
  2070. Common Lisp at all. This may be a topic worth standardizing under separate
  2071. cover. Here is some speculation about some possible things that might happen.
  2072.  
  2073. An implementation might simply call abort or break directly without signaling
  2074. any condition.
  2075.  
  2076. Another implementation might signal some condition related to the fact that a
  2077. key had been pressed rather than to the action that should be taken. This is
  2078. one way to allow user customization. Perhaps there would be an
  2079. implementation-dependent keyboard-interrupt condition type with a slot
  2080. containing the key that was pressed-or perhaps there would be such a condition
  2081. type, but rather than its having slots, different subtypes of that type with
  2082. names like keyboard-abort, keyboard-break, and so on might be signaled. That
  2083. implementation would then document the action it would take if user programs
  2084. failed to handle the condition, and perhaps ways for user programs to usefully
  2085. dismiss the interrupt.
  2086. -------------------------------------------------------------------------------
  2087. Implementation note: Implementors are encouraged to make sure that there is
  2088. always a restart named abort around any user code so that user code can call
  2089. abort at any time and expect something reasonable to happen; exactly what the
  2090. reasonable thing is may vary somewhat. Typically, in an interactive program,
  2091. invoking abort should return the user to top level, though in some batch or
  2092. multi-processing situations killing the running process might be more
  2093. appropriate.
  2094. -------------------------------------------------------------------------------
  2095.  
  2096. [Macro]
  2097.  
  2098. restart-case expression {(case-name arglist
  2099.                            {keyword value}*
  2100.                            {form}*)}*
  2101.  
  2102. The expression is evaluated in a dynamic context where the clauses have special
  2103. meanings as points to which control may be transferred. If the expression
  2104. finishes executing and returns any values, all such values are simply returned
  2105. by the restart-case form. While the expression is running, any code may
  2106. transfer control to one of the clauses (see invoke-restart). If a transfer
  2107. occurs, the forms in the body of that clause will be evaluated and any values
  2108. returned by the last such form will be returned by the restart-case form.
  2109.  
  2110. As a special case, if the expression is a list whose car is signal, error,
  2111. cerror, or warn, then with-condition-restarts is implicitly used to associate
  2112. the restarts with the condition to be signaled. For example,
  2113.  
  2114. (restart-case (signal weird-error)
  2115.   (become-confused ...)
  2116.   (rewind-line-printer ...)
  2117.   (halt-and-catch-fire ...))
  2118.  
  2119. is equivalent to
  2120.  
  2121. (restart-case (with-condition-restarts
  2122.                 weird-error
  2123.                 (list (find-restart 'become-confused)
  2124.                       (find-restart 'rewind-line-printer)
  2125.                       (find-restart 'halt-and-catch-fire))
  2126.                 (signal weird-error))
  2127.   (become-confused ...)
  2128.   (rewind-line-printer ...)
  2129.   (halt-and-catch-fire ...))
  2130.  
  2131. If there are no forms in a selected clause, restart-case returns nil.
  2132.  
  2133. The case-name may be nil or a symbol naming this restart.
  2134.  
  2135. It is possible to have more than one clause use the same case-name. In this
  2136. case, the first clause with that name will be found by find-restart. The other
  2137. clauses are accessible using compute-restarts. [In this respect, restart-case
  2138. is rather different from case!-GLS]
  2139.  
  2140. Each arglist is a normal lambda-list containing parameters to be bound during
  2141. the execution of its corresponding forms. These parameters are used to pass any
  2142. necessary data from a call to invoke-restart to the restart-case clause.
  2143.  
  2144. By default, invoke-restart-interactively will pass no arguments and all
  2145. parameters must be optional in order to accommodate interactive restarting.
  2146. However, the parameters need not be optional if the :interactive keyword has
  2147. been used to inform invoke-restart-interactively about how to compute a proper
  2148. argument list.
  2149.  
  2150. The valid keyword value pairs are the following:
  2151.  
  2152. :test fn
  2153.  
  2154.      The fn must be a suitable argument for the function special form. The
  2155.      expression (function fn) will be evaluated in the current lexical
  2156.      environment. It should produce a function of one argument, a condition. If
  2157.      this function returns nil when given some condition, functions such as
  2158.      find-restart, compute-restart, and invoke-restart will not consider this
  2159.      restart when searching for restarts associated with that condition. If
  2160.      this pair is not supplied, it is as if
  2161.  
  2162.      (lambda (c) (declare (ignore c)) t)
  2163.  
  2164.      were used for the fn.
  2165.  
  2166. :interactive fn
  2167.  
  2168.      The fn must be a suitable argument for the function special form. The
  2169.      expression (function fn) will be evaluated in the current lexical
  2170.      environment. It should produce a function of no arguments that returns
  2171.      arguments to be used by invoke-restart-interactively when invoking this
  2172.      function. This function will be called in the dynamic environment
  2173.      available prior to any restart attempt. It may interact with the user on
  2174.      the stream in *query-io*.
  2175.  
  2176.      If a restart is invoked interactively but no :interactive option was
  2177.      supplied, the argument list used in the invocation is the empty list.
  2178.  
  2179. :report exp
  2180.  
  2181.      If exp is not a literal string, it must be a suitable argument to the
  2182.      function special form. The expression (function exp) will be evaluated in
  2183.      the current lexical environment. It should produce a function of one
  2184.      argument, a stream, that prints on the stream a description of the
  2185.      restart. This function is called whenever the restart is printed while
  2186.      *print-escape* is nil.
  2187.  
  2188.      If exp is a literal string, it is shorthand for
  2189.  
  2190.      (lambda (s) (write-string exp s))
  2191.  
  2192.      [That is, a function is provided that will simply write the given string
  2193.      literally to the stream.-GLS]
  2194.  
  2195.      If a named restart is asked to report but no report information has been
  2196.      supplied, the name of the restart is used in generating default report
  2197.      text.
  2198.  
  2199.      When *print-escape* is nil, the printer will use the report information
  2200.      for a restart. For example, a debugger might announce the action of typing
  2201.      ``:continue'' by executing the equivalent of
  2202.  
  2203.      (format *debug-io* "~&~S - ~A~%" ':continue some-restart)
  2204.  
  2205.      which might then display as something like
  2206.  
  2207.      :CONTINUE - Return to command level.
  2208.  
  2209.      It is an error if an unnamed restart is used and no report information is
  2210.      provided.
  2211.  
  2212. -------------------------------------------------------------------------------
  2213. Rationale: Unnamed restarts are required to have report information on the
  2214. grounds that they are generally only useful interactively, and an interactive
  2215. option that has no description is of little value.
  2216. -------------------------------------------------------------------------------
  2217. Implementation note: Implementations are encouraged to warn about this error at
  2218. compilation time.
  2219.  
  2220. At run time, this error might be noticed when entering the debugger. Since
  2221. signaling an error would probably cause recursive entry into the debugger
  2222. (causing yet another recursive error, and so on), it is suggested that the
  2223. debugger print some indication of such problems when they occur, but not
  2224. actually signal errors.
  2225. -------------------------------------------------------------------------------
  2226.  
  2227. Note that
  2228.  
  2229. (restart-case expression
  2230.   (name1 arglist1 options1 . body1)
  2231.   (name2 arglist2 options2 . body2)
  2232.   ...)
  2233.  
  2234. is essentially equivalent to
  2235.  
  2236. (block #1=#:block-1
  2237.   (let ((#2=#:var-2 nil))
  2238.     (tagbody
  2239.       (restart-bind ((name1 #'(lambda (&rest temp)
  2240.                     (setq #2# temp)
  2241.                     (go #3=#:tag-3))
  2242.                 <slightly transformed options1>)
  2243.                      (name2 #'(lambda (&rest temp)
  2244.                     (setq #2# temp)
  2245.                     (go #4=#:tag-4))
  2246.                 <slightly transformed options2>)
  2247.                      ...)
  2248.         (return-from #1# expression))
  2249.       #3# (return-from #1#
  2250.                 (apply #'(lambda arglist1 . body1) #2#))
  2251.       #4# (return-from #1#
  2252.                 (apply #'(lambda arglist2 . body2) #2#))
  2253.       ...)))
  2254.  
  2255. [Note the use of ``gensyms'' such as #:block-1 as block names, variables, and
  2256. tagbody tags in this example, and the use of #n= and #n# read-macro syntax to
  2257. indicate that the very same gensym appears in multiple places.-GLS]
  2258.  
  2259. Here are some examples of the use of restart-case.
  2260.  
  2261. (loop
  2262.   (restart-case (return (apply function some-args))
  2263.     (new-function (new-function)
  2264.         :report "Use a different function."
  2265.         :interactive
  2266.           (lambda ()
  2267.             (list (prompt-for 'function "Function: ")))
  2268.       (setq function new-function))))
  2269.  
  2270. (loop
  2271.   (restart-case (return (apply function some-args))
  2272.     (nil (new-function)
  2273.         :report "Use a different function."
  2274.         :interactive
  2275.           (lambda ()
  2276.             (list (prompt-for 'function "Function: ")))
  2277.       (setq function new-function))))
  2278.  
  2279. (restart-case (a-command-loop)
  2280.   (return-from-command-level ()
  2281.       :report
  2282.         (lambda (s)     ;Argument s is a stream
  2283.           (format s "Return from command level ~D." level))
  2284.     nil))
  2285.  
  2286. (loop
  2287.   (restart-case (another-random-computation)
  2288.     (continue () nil)))
  2289.  
  2290. The first and second examples are equivalent from the point of view of someone
  2291. using the interactive debugger, but they differ in one important aspect for
  2292. non-interactive handling. If a handler ``knows about'' named restarts, as in,
  2293. for example,
  2294.  
  2295. (when (find-restart 'new-function)
  2296.   (invoke-restart 'new-function the-replacement))
  2297.  
  2298. then only the first example, and not the second, will have control transferred
  2299. to its correction clause, since only the first example uses a restart named
  2300. new-function.
  2301.  
  2302. Here is a more complete example:
  2303.  
  2304. (let ((my-food 'milk)
  2305.       (my-color 'greenish-blue))
  2306.   (do ()
  2307.       ((not (bad-food-color-p my-food my-color)))
  2308.     (restart-case (error 'bad-food-color
  2309.                          :food my-food :color my-color)
  2310.       (use-food (new-food)
  2311.           :report "Use another food."
  2312.         (setq my-food new-food))
  2313.       (use-color (new-color)
  2314.           :report "Use another color."
  2315.         (setq my-color new-color))))
  2316.   ;; We won't get to here until MY-FOOD
  2317.   ;; and MY-COLOR are compatible.
  2318.   (list my-food my-color))
  2319.  
  2320. Assuming that use-food and use-color have been defined as
  2321.  
  2322. (defun use-food (new-food)
  2323.   (invoke-restart 'use-food new-food))
  2324.  
  2325. (defun use-color (new-color)
  2326.   (invoke-restart 'use-color new-color))
  2327.  
  2328. a handler can then restart from the error in either of two ways. It may correct
  2329. the color or correct the food. For example:
  2330.  
  2331. #'(lambda (c) ... (use-color 'white) ...)   ;Corrects color
  2332.  
  2333. #'(lambda (c) ... (use-food 'cheese) ...)   ;Corrects food
  2334.  
  2335. Here is an example using handler-bind and restart-case that refers to a
  2336. condition type foo-error, presumably defined elsewhere:
  2337.  
  2338. (handler-bind ((foo-error #'(lambda (ignore) (use-value 7))))
  2339.   (restart-case (error 'foo-error)
  2340.     (use-value (x) (* x x))))
  2341.  => 49
  2342.  
  2343. [Macro]
  2344. restart-bind ({(name function {keyword value}*)}*) {form}*
  2345.  
  2346. Executes a body of forms in a dynamic context where the given restart bindings
  2347. are in effect.
  2348.  
  2349. Each name may be nil to indicate an anonymous restart, or some other symbol to
  2350. indicate a named restart.
  2351.  
  2352. Each function is a form that should evaluate to a function to be used to
  2353. perform the restart. If invoked, this function may either perform a non-local
  2354. transfer of control or it may return normally. The function may take whatever
  2355. arguments the programmer feels are appropriate; it will be invoked only if
  2356. invoke-restart is used from a program, or if a user interactively asks the
  2357. debugger to invoke it. In the case of interactive invocation, the
  2358. :interactive-function option is used.
  2359.  
  2360. The valid keyword value pairs are as follows:
  2361.  
  2362. :test-function form
  2363.  
  2364.      The form will be evaluated in the current lexical environment and should
  2365.      return a function of one argument, a condition. If this function returns
  2366.      nil when given some condition, functions such as find-restart,
  2367.      compute-restart, and invoke-restart will not consider this restart when
  2368.      searching for restarts associated with that condition. If this pair is not
  2369.      supplied, it is as if
  2370.  
  2371.      #'(lambda (c) (declare (ignore c)) t)
  2372.  
  2373.      were used for the form.
  2374.  
  2375. :interactive-function form
  2376.  
  2377.      The form will be evaluated in the current lexical environment and should
  2378.      return a function of no arguments that constructs a list of arguments to
  2379.      be used by invoke-restart-interactively when invoking this restart. The
  2380.      function may prompt interactively using *query-io* if necessary.
  2381.  
  2382. :report-function form
  2383.  
  2384.      The form will be evaluated in the current lexical environment and should
  2385.      return a function of one argument, a stream, that prints on the stream a
  2386.      summary of the action this restart will take. This function is called
  2387.      whenever the restart is printed while *print-escape* is nil.
  2388.  
  2389. [Macro]
  2390.  
  2391. with-condition-restarts condition-form restarts-form
  2392.       {declaration}* {form}*
  2393.  
  2394. The value of condition-form should be a condition C and the value of
  2395. restarts-form should be a list of restarts (R1 R2 ...). The forms of the body
  2396. are evaluated as an implicit progn. While in the dynamic context of the body,
  2397. an attempt to find a restart associated with a particular condition C' will
  2398. consider the restarts R1, R2, ... if C' is eq to C.
  2399.  
  2400. Usually this macro is not used explicitly in code, because restart-case handles
  2401. most of the common uses in a way that is syntactically more concise.
  2402.  
  2403. [The X3J13 vote (CONDITION-RESTARTS)   left it unclear whether
  2404. with-condition-restarts permits declarations to appear at the heads of its
  2405. body. I believe that was the intent, but this is only my interpretation.-GLS]
  2406. [change_end]
  2407.  
  2408. -------------------------------------------------------------------------------
  2409.  
  2410. 29.4.8. Finding and Manipulating Restarts
  2411.  
  2412. [change_begin]
  2413. The following functions determine what restarts are active and invoke restarts.
  2414.  
  2415. [Function]
  2416. compute-restarts &optional condition
  2417.  
  2418. Uses the dynamic state of the program to compute a list of the restarts that
  2419. are currently active. See restart-bind.
  2420.  
  2421. If condition is nil or not supplied, all outstanding restarts are returned. If
  2422. condition is not nil, only restarts associated with that condition are
  2423. returned.
  2424.  
  2425. Each restart represents a function that can be called to perform some form of
  2426. recovery action, usually a transfer of control to an outer point in the running
  2427. program. Implementations are free to implement these objects in whatever manner
  2428. is most convenient; the objects need have only dynamic extent (relative to the
  2429. scope of the binding form that instantiates them).
  2430.  
  2431. The list that results from a call to compute-restarts is ordered so that the
  2432. inner (that is, more recently established) restarts are nearer the head of the
  2433. list.
  2434.  
  2435. Note, too, that compute-restarts returns all valid restarts, including
  2436. anonymous ones, even if some of them have the same name as others and would
  2437. therefore not be found by find-restart when given a symbol argument.
  2438.  
  2439. Implementations are permitted, but not required, to return different (that is,
  2440. non-eq) lists from repeated calls to compute-restarts while in the same dynamic
  2441. environment. It is an error to modify the list that is returned by
  2442. compute-restarts.
  2443.  
  2444. [Function]
  2445. restart-name restart
  2446.  
  2447. Returns the name of the given restart, or nil if it is not named.
  2448.  
  2449. [Function]
  2450. find-restart restart-identifier &optional condition
  2451.  
  2452. Searches for a particular restart in the current dynamic environment.
  2453.  
  2454. If condition is nil or not supplied, all outstanding restarts are considered.
  2455. If condition is not nil, only restarts associated with that condition are
  2456. considered.
  2457.  
  2458. If the restart-identifier is a non-nil symbol, then the innermost (that is,
  2459. most recently established) restart with that name is returned; nil is returned
  2460. if no such restart is found.
  2461.  
  2462. If restart-identifier is a restart object, then it is simply returned, unless
  2463. it is not currently active, in which case nil is returned.
  2464.  
  2465. Although anonymous restarts have a name of nil, it is an error for the symbol
  2466. nil to be given as the restart-identifier. Applications that would seem to
  2467. require this should be rewritten to make appropriate use of compute-restarts
  2468. instead.
  2469.  
  2470. [Function]
  2471. invoke-restart restart-identifier &rest arguments
  2472.  
  2473. Calls the function associated with the given restart-identifier, passing any
  2474. given arguments. The restart-identifier must be a restart or the non-null name
  2475. of a restart that is valid in the current dynamic context. If the argument is
  2476. not valid, an error of type control-error will be signaled.
  2477.  
  2478. -------------------------------------------------------------------------------
  2479. Implementation note: Restart functions call this function, not vice versa.
  2480. -------------------------------------------------------------------------------
  2481.  
  2482. [Function]
  2483. invoke-restart-interactively restart-identifier
  2484.  
  2485. Calls the function associated with the given restart-identifier, prompting for
  2486. any necessary arguments. The restart-identifier must be a restart or the
  2487. non-null name of a restart that is valid in the current dynamic context. If the
  2488. argument is not valid, an error of type control-error will be signaled.
  2489.  
  2490. The function invoke-restart-interactively will prompt for arguments by
  2491. executing the code provided in the :interactive keyword to restart-case or
  2492. :interactive-function keyword to restart-bind.
  2493.  
  2494. If no :interactive or :interactive-function option has been supplied in the
  2495. corresponding restart-case or restart-bind, then it is an error if the restart
  2496. takes required arguments. If the arguments are optional, an empty argument list
  2497. will be used in this case.
  2498.  
  2499. Once invoke-restart-interactively has calculated the arguments, it simply
  2500. performs (apply #'invoke-restart restart-identifier arguments).
  2501.  
  2502. invoke-restart-interactively is used internally by the debugger and may also be
  2503. useful in implementing other portable, interactive debugging tools.
  2504. [change_end]
  2505.  
  2506. -------------------------------------------------------------------------------
  2507.  
  2508. 29.4.9. Warnings
  2509.  
  2510. [change_begin]
  2511. Warnings are a subclass of errors that are conventionally regarded as ``mild.''
  2512.  
  2513. [Function]
  2514. warn datum &rest arguments
  2515.  
  2516. [This supersedes the description of warn given in section 24.1.-GLS]
  2517.  
  2518. Warns about a situation, by signaling a condition of type warning.
  2519.  
  2520. If datum is a condition, then that condition is used directly. In this case, if
  2521. the condition is not of type warning or arguments is non-nil, an error of type
  2522. type-error is signaled.
  2523.  
  2524. If datum is a condition type (a class or class name), then the condition used
  2525. is effectively the result of (apply #'make-condition datum arguments). This
  2526. result must be of type warning or an error of type type-error is signaled.
  2527.  
  2528. If datum is a string, then the condition used is effectively the result of
  2529.  
  2530. (make-condition 'simple-error
  2531.                 :format-string datum
  2532.                 :format-arguments arguments)
  2533.  
  2534. The precise mechanism for warning is as follows.
  2535.  
  2536.   1.  The warning condition is signaled.
  2537.  
  2538.      While the warning condition is being signaled, the muffle-warning restart
  2539.      is established for use by a handler to bypass further action by warn (that
  2540.      is, to cause warn to immediately return nil).
  2541.  
  2542.      As part of the signaling process, if (typep condition *break-on-signals*)
  2543.      is true, then a break will occur prior to beginning the signaling process.
  2544.  
  2545.   2.  If no handlers for the warning condition are found, or if all such
  2546.      handlers decline, then the condition will be reported to *error-output* by
  2547.      the warn function (with possible implementation-specific extra output such
  2548.      as motion to a fresh line before or after the display of the warning, or
  2549.      supplying some introductory text mentioning the name of the function that
  2550.      called warn or the fact that this is a warning).
  2551.  
  2552.   3.  The value returned by warn (if it returns) is nil.
  2553.  
  2554. [change_end]
  2555.  
  2556. -------------------------------------------------------------------------------
  2557.  
  2558. 29.4.10. Restart Functions
  2559.  
  2560. [change_begin]
  2561. Common Lisp has the following restart functions built in.
  2562.  
  2563. [Function]
  2564. abort &optional condition
  2565.  
  2566. This function transfers control to the restart named abort. If no such restart
  2567. exists, abort signals an error of type control-error.
  2568.  
  2569. If condition is nil or not supplied, all outstanding restarts are considered.
  2570. If condition is not nil, only restarts associated with that condition are
  2571. considered.
  2572.  
  2573. The purpose of the abort restart is generally to allow control to return to the
  2574. innermost ``command level.''
  2575.  
  2576. [Function]
  2577. continue &optional condition
  2578.  
  2579. This function transfers control to the restart named continue. If no such
  2580. restart exists, continue returns nil.
  2581.  
  2582. If condition is nil or not supplied, all outstanding restarts are considered.
  2583. If condition is not nil, only restarts associated with that condition are
  2584. considered.
  2585.  
  2586. The continue restart is generally part of simple protocols where there is a
  2587. single ``obvious'' way to continue, as with break and cerror. Some user-defined
  2588. protocols may also wish to incorporate it for similar reasons. In general,
  2589. however, it is more reliable to design a special-purpose restart with a name
  2590. that better suits the particular application.
  2591.  
  2592. [Function]
  2593. muffle-warning &optional condition
  2594.  
  2595. This function transfers control to the restart named muffle-warning. If no such
  2596. restart exists, muffle-warning signals an error of type control-error.
  2597.  
  2598. If condition is nil or not supplied, all outstanding restarts are considered.
  2599. If condition is not nil, only restarts associated with that condition are
  2600. considered.
  2601.  
  2602. warn sets up this restart so that handlers of warning conditions have a way to
  2603. tell warn that a warning has already been dealt with and that no further action
  2604. is warranted.
  2605.  
  2606. [Function]
  2607. store-value value &optional condition
  2608.  
  2609. This function transfers control (and one value) to the restart named
  2610. store-value. If no such restart exists, store-value returns nil.
  2611.  
  2612. If condition is nil or not supplied, all outstanding restarts are considered.
  2613. If condition is not nil, only restarts associated with that condition are
  2614. considered.
  2615.  
  2616. The store-value restart is generally used by handlers trying to recover from
  2617. errors of types such as cell-error or type-error, where the handler may wish to
  2618. supply a replacement datum to be stored permanently.
  2619.  
  2620. [Function]
  2621. use-value value &optional condition
  2622.  
  2623. This function transfers control (and one value) to the restart named use-value.
  2624. If no such restart exists, use-value returns nil.
  2625.  
  2626. If condition is nil or not supplied, all outstanding restarts are considered.
  2627. If condition is not nil, only restarts associated with that condition are
  2628. considered.
  2629.  
  2630. The use-value restart is generally used by handlers trying to recover from
  2631. errors of types such as cell-error, where the handler may wish to supply a
  2632. replacement datum for one-time use.
  2633. [change_end]
  2634.  
  2635. -------------------------------------------------------------------------------
  2636.  
  2637. 29.4.11. Debugging Utilities
  2638.  
  2639. [change_begin]
  2640. Common Lisp does not specify exactly what a debugger is or does, but it does
  2641. provide certain means for indicating intent to transfer control to a
  2642. supervisory or debugging facility.
  2643.  
  2644. [Function]
  2645. break &optional format-string &rest format-arguments
  2646.  
  2647. [This supersedes the description of break given in section 24.1.-GLS]
  2648.  
  2649. The function break prints the message described by the format-string and
  2650. format-arguments and then goes directly into the debugger without allowing any
  2651. possibility of interception by programmed error-handling facilities.
  2652.  
  2653. If no format-string is supplied, a suitable default will be generated.
  2654.  
  2655. If continued, break returns nil.
  2656.  
  2657. Note that break is presumed to be used as a way of inserting temporary
  2658. debugging ``breakpoints'' in a program, not as a way of signaling errors; it is
  2659. expected that continuing from a break will not trigger any unusual recovery
  2660. action. For this reason, break does not take the additional format control
  2661. string that cerror takes as its first argument. This and the lack of any
  2662. possibility of interception by programmed error handling are the only
  2663. program-visible differences between break and cerror. The user interface
  2664. aspects of these functions are permitted to vary more widely; for example, it
  2665. is permissible for a read-eval-print loop to be entered by break rather than by
  2666. the conventional debugger.
  2667.  
  2668. break could be defined by
  2669.  
  2670. (defun break (&optional (format-string "Break")
  2671.               &rest format-arguments)
  2672.   (with-simple-restart (continue "Return from BREAK.")
  2673.     (invoke-debugger
  2674.       (make-condition 'simple-condition
  2675.                       :format-string format-string
  2676.                       :format-arguments format-arguments)))
  2677.   nil)
  2678.  
  2679. [Function]
  2680. invoke-debugger condition
  2681.  
  2682. Attempts interactive handling of its argument, which must be a condition.
  2683.  
  2684. If the variable *debugger-hook* is not nil, it will be called as a function on
  2685. two arguments: the condition being handled and the value of *debugger-hook*. If
  2686. a hook function returns normally, the standard debugger will be tried.
  2687.  
  2688. The standard debugger will never directly return. Return can occur only by a
  2689. special transfer of control, such as the use of a restart.
  2690.  
  2691. -------------------------------------------------------------------------------
  2692. Remark: The exact way in which the debugger interacts with users is expected to
  2693. vary considerably from system to system. For example, some systems may use a
  2694. keyboard interface, while others may use a mouse interface. Of those systems
  2695. using keyboard commands, some may use single-character commands and others may
  2696. use parsed line-at-a-time commands. The exact set of commands will vary as
  2697. well. The important properties of a debugger are that it makes information
  2698. about the error accessible and that it makes the set of apparent restarts
  2699. easily accessible.
  2700.  
  2701. It is desirable to have a mode where the debugger allows other features, such
  2702. as the ability to inspect data, stacks, etc. However, it may sometimes be
  2703. appropriate to have this kind of information hidden from users. Experience on
  2704. the Lisp Machines has shown that some users who are not programmers develop a
  2705. terrible phobia of debuggers. The reason for this usually may be traced to the
  2706. fact that the debugger is very foreign to them and provides an overwhelming
  2707. amount of information of interest only to programmers. With the advent of
  2708. restarts, there is a clear mechanism for the construction of ``friendly''
  2709. debuggers. Programmers can be taught how to get to the information they need
  2710. for debugging, but it should be possible to construct user interfaces to the
  2711. debugger that are natural, convenient, intelligible, and friendly even to
  2712. non-programmers.
  2713. -------------------------------------------------------------------------------
  2714.  
  2715. [Variable]
  2716. *debugger-hook*
  2717.  
  2718. This variable should hold either nil or a function of two arguments, a
  2719. condition and the value of *debugger-hook*. This function may either handle the
  2720. condition (transfer control) or return normally (allowing the standard debugger
  2721. to run).
  2722.  
  2723. Note that, to minimize recursive errors while debugging, *debugger-hook* is
  2724. bound to nil when calling this function. When evaluating code typed in by the
  2725. user interactively, the hook function may want to bind *debugger-hook* to the
  2726. function that was its second argument so that recursive errors can be handled
  2727. using the same interactive facility.
  2728. [change_end]
  2729.  
  2730. -------------------------------------------------------------------------------
  2731.  
  2732. 29.5. Predefined Condition Types
  2733.  
  2734. [change_begin]
  2735. [The proposal for the Common Lisp Condition System introduced a new notation
  2736. for documenting types, treating them in the same syntactic manner as functions
  2737. and variables. This notation is used in this section but is not reflected
  2738. throughout the entire book.-GLS]
  2739.  
  2740. X3J13 voted in March 1989 (ZLOS-CONDITIONS)   to integrate the Condition System
  2741. and the Object System. All condition types are CLOS classes and all condition
  2742. objects are ordinary CLOS objects.
  2743.  
  2744. [Type]
  2745. restart
  2746.  
  2747. This is the data type used to represent a restart.
  2748.  
  2749.  
  2750. ----------------------------------------------------------------
  2751. Table 29-1: Condition Type Hierarchy
  2752.  
  2753. condition
  2754.     simple-condition
  2755.     serious-condition
  2756.         error
  2757.             simple-error
  2758.             arithmetic-error
  2759.                 division-by-zero
  2760.                 floating-point-overflow
  2761.                 floating-point-underflow
  2762.                 ...
  2763.             cell-error
  2764.                 unbound-variable
  2765.                 undefined-function
  2766.                 ...
  2767.             control-error
  2768.             file-error
  2769.             package-error
  2770.             program-error
  2771.             stream-error
  2772.                 end-of-file
  2773.                 ...
  2774.             type-error
  2775.                 simple-type-error
  2776.                 ...
  2777.             ...
  2778.         storage-condition
  2779.         ...
  2780.     warning
  2781.         simple-warning
  2782.         ...
  2783.     ...
  2784.  
  2785. ----------------------------------------------------------------
  2786.  
  2787. The Common Lisp condition type hierarchy is illustrated in table 29-1.
  2788.  
  2789. The types that are not leaves in the hierarchy (that is, condition, warning,
  2790. storage-condition, error, arithmetic-error, control-error, and so on) are
  2791. provided primarily for type inclusion purposes. Normally they would not be
  2792. directly instantiated.
  2793.  
  2794. Implementations are permitted to support non-portable synonyms for these types,
  2795. as well as to introduce other types that are above, below, or between the types
  2796. shown in this tree as long as the indicated subtype relationships are not
  2797. violated.
  2798.  
  2799. The types simple-condition, serious-condition, and warning are pairwise
  2800. disjoint. The type error is also disjoint from types simple-condition and
  2801. warning.
  2802.  
  2803. [Type]
  2804. condition
  2805.  
  2806. All types of conditions, whether error or non-error, must inherit from this
  2807. type.
  2808.  
  2809. [Type]
  2810. warning
  2811.  
  2812. All types of warnings should inherit from this type. This is a subtype of
  2813. condition.
  2814.  
  2815. [Type]
  2816. serious-condition
  2817.  
  2818. All serious conditions (conditions serious enough to require interactive
  2819. intervention if not handled) should inherit from this type. This is a subtype
  2820. of condition.
  2821.  
  2822. This condition type is provided primarily for terminological convenience. In
  2823. fact, signaling a condition that inherits from serious-condition does not force
  2824. entry into the debugger. Rather, it is conventional to use error (or something
  2825. built on error) to signal conditions that are of this type, and to use signal
  2826. to signal conditions that are not of this type.
  2827.  
  2828. [Type]
  2829. error
  2830.  
  2831. All types of error conditions inherit from this condition. This is a subtype of
  2832. serious-condition.
  2833.  
  2834. The default condition type for signal and warn is simple-condition. The default
  2835. condition type for error and cerror is simple-error.
  2836.  
  2837. [Type]
  2838. simple-condition
  2839.  
  2840. Conditions signaled by signal when given a format string as a first argument
  2841. are of this type. This is a subtype of condition. The initialization keywords
  2842. :format-string and :format-arguments are supported to initialize the slots,
  2843. which can be accessed using simple-condition-format-string and
  2844. simple-condition-format-arguments. If :format-arguments is not supplied to
  2845. make-condition, the format-arguments slot defaults to nil.
  2846.  
  2847. [Type]
  2848. simple-warning
  2849.  
  2850. Conditions signaled by warn when given a format string as a first argument are
  2851. of this type. This is a subtype of warning. The initialization keywords
  2852. :format-string and :format-arguments are supported to initialize the slots,
  2853. which can be accessed using simple-condition-format-string and
  2854. simple-condition-format-arguments. If :format-arguments is not supplied to
  2855. make-condition, the format-arguments slot defaults to nil.
  2856.  
  2857. In implementations supporting multiple inheritance, this type will also be a
  2858. subtype of simple-condition.
  2859.  
  2860. [Type]
  2861. simple-error
  2862.  
  2863. Conditions signaled by error and cerror when given a format string as a first
  2864. argument are of this type. This is a subtype of error. The initialization
  2865. keywords :format-string and :format-arguments are supported to initialize the
  2866. slots, which can be accessed using simple-condition-format-string and
  2867. simple-condition-format-arguments. If :format-arguments is not supplied to
  2868. make-condition, the format-arguments slot defaults to nil.
  2869.  
  2870. In implementations supporting multiple inheritance, this type will also be a
  2871. subtype of simple-condition.
  2872.  
  2873. [Function]
  2874. simple-condition-format-string condition
  2875.  
  2876. Accesses the format-string slot of a given condition, which must be of type
  2877. simple-condition, simple-warning, simple-error, or simple-type-error.
  2878.  
  2879. [Function]
  2880. simple-condition-format-arguments condition
  2881.  
  2882. Accesses the format-arguments slot of a given condition, which must be of type
  2883. simple-condition, simple-warning, simple-error, or simple-type-error.
  2884.  
  2885. [Type]
  2886. storage-condition
  2887.  
  2888. Conditions that relate to storage overflow should inherit from this type. This
  2889. is a subtype of serious-condition.
  2890.  
  2891. [Type]
  2892. type-error
  2893.  
  2894. Errors in the transfer of data in a program should inherit from this type. This
  2895. is a subtype of error. For example, conditions to be signaled by check-type
  2896. should inherit from this type. The initialization keywords :datum and
  2897. :expected-type are supported to initialize the slots, which can be accessed
  2898. using type-error-datum and type-error-expected-type.
  2899.  
  2900. [Function]
  2901. type-error-datum condition
  2902.  
  2903. Accesses the datum slot of a given condition, which must be of type type-error.
  2904.  
  2905. [Function]
  2906. type-error-expected-type condition
  2907.  
  2908. Accesses the expected-type slot of a given condition, which must be of type
  2909. type-error. Users of type-error conditions are expected to fill this slot with
  2910. an object that is a valid Common Lisp type specifier.
  2911.  
  2912. [Type]
  2913. simple-type-error
  2914.  
  2915. Conditions signaled by facilities similar to check-type may want to use this
  2916. type. The initialization keywords :format-string and :format-arguments are
  2917. supported to initialize the slots, which can be accessed using
  2918. simple-condition-format-string and simple-condition-format-arguments. If
  2919. :format-arguments is not supplied to make-condition, the format-arguments slot
  2920. defaults to nil.
  2921.  
  2922. In implementations supporting multiple inheritance, this type will also be a
  2923. subtype of simple-condition.
  2924.  
  2925. [Type]
  2926. program-error
  2927.  
  2928. Errors relating to incorrect program syntax that are statically detectable
  2929. should inherit from this type (regardless of whether they are in fact
  2930. statically detected). This is a subtype of error. This is not a subtype of
  2931. control-error.
  2932.  
  2933. [Type]
  2934. control-error
  2935.  
  2936. Errors in the dynamic transfer of control in a program should inherit from this
  2937. type. This is a subtype of error. This is not a subtype of program-error.
  2938.  
  2939. The errors that result from giving throw a tag that is not active or from
  2940. giving go or return-from a tag that is no longer dynamically available are
  2941. control errors.
  2942.  
  2943. On the other hand, the errors that result from naming a go tag or return-from
  2944. tag that is not lexically apparent are not control errors. They are program
  2945. errors. See program-error.
  2946.  
  2947. [Type]
  2948. package-error
  2949.  
  2950. Errors that occur during operations on packages should inherit from this type.
  2951. This is a subtype of error. The initialization keyword :package is supported to
  2952. initialize the slot, which can be accessed using package-error-package.
  2953.  
  2954. [Function]
  2955. package-error-package condition
  2956.  
  2957. Accesses the package (or package name) that was being modified or manipulated
  2958. in a condition of type package-error.
  2959.  
  2960. [Type]
  2961. stream-error
  2962.  
  2963. Errors that occur during input from, output to, or closing a stream should
  2964. inherit from this type. This is a subtype of error. The initialization keyword
  2965. :stream is supported to initialize the slot, which can be accessed using
  2966. stream-error-stream.
  2967.  
  2968. [Function]
  2969. stream-error-stream condition
  2970.  
  2971. Accesses the offending stream of a condition of type stream-error.
  2972.  
  2973. [Type]
  2974. end-of-file
  2975.  
  2976. The error that results when a read operation is done on a stream that has no
  2977. more tokens or characters should inherit from this type. This is a subtype of
  2978. stream-error.
  2979.  
  2980. [Type]
  2981. file-error
  2982.  
  2983. Errors that occur during an attempt to open a file, or during some low-level
  2984. transaction with a file system, should inherit from this type. This is a
  2985. subtype of error. The initialization keyword :pathname is supported to
  2986. initialize the slot, which can be accessed using file-error-pathname.
  2987.  
  2988. [Function]
  2989. file-error-pathname condition
  2990.  
  2991. Accesses the offending pathname of a condition of type file-error.
  2992.  
  2993. [Type]
  2994. cell-error
  2995.  
  2996. Errors that occur while accessing a location should inherit from this type.
  2997. This is a subtype of error. The initialization keyword :name is supported to
  2998. initialize the slot, which can be accessed using cell-error-name.
  2999.  
  3000. [Function]
  3001. cell-error-name condition
  3002.  
  3003. Accesses the offending cell name of a condition of type cell-error.
  3004.  
  3005. [Type]
  3006. unbound-variable
  3007.  
  3008. The error that results from trying to access the value of an unbound variable
  3009. should inherit from this type. This is a subtype of cell-error.
  3010.  
  3011. [Type]
  3012. undefined-function
  3013.  
  3014. The error that results from trying to access the value of an undefined function
  3015. should inherit from this type. This is a subtype of cell-error.
  3016.  
  3017. -------------------------------------------------------------------------------
  3018. Remark: [Note: This remark was written well before the vote by X3J13 in June
  3019. 1988 (CLOS)   to add the Common Lisp Object System to the forthcoming draft
  3020. standard (see chapter 28) and the vote to integrate the Condition System and
  3021. the Object System. I have retained the remark here for reasons of historical
  3022. interest.-GLS]
  3023.  
  3024. Some readers may wonder why undefined-function is not defined to inherit from
  3025. some condition such as control-error. The answer is that any such arrangement
  3026. would require the presence of multiple inheritance-a luxury we do not currently
  3027. have (without resorting to deftype, which we are currently avoiding). When the
  3028. Common Lisp Object System comes into being, we might want to consider issues
  3029. like this. Multiple inheritance makes a lot of things in a condition system
  3030. much more flexible to deal with.
  3031. -------------------------------------------------------------------------------
  3032.  
  3033. [Type]
  3034. arithmetic-error
  3035.  
  3036. Errors that occur while doing arithmetic type operations should inherit from
  3037. this type. This is a subtype of error. The initialization keywords :operation
  3038. and :operands are supported to initialize the slots, which can be accessed
  3039. using arithmetic-error-operation and arithmetic-error-operands.
  3040.  
  3041. [Function]
  3042. arithmetic-error-operation condition
  3043.  
  3044. Accesses the offending operation of a condition of type arithmetic-error.
  3045.  
  3046. [Function]
  3047. arithmetic-error-operands condition
  3048.  
  3049. Accesses a list of the offending operands in a condition of type
  3050. arithmetic-error.
  3051.  
  3052. [Type]
  3053. division-by-zero
  3054.  
  3055. Errors that occur because of division by zero should inherit from this type.
  3056. This is a subtype of arithmetic-error.
  3057.  
  3058. [Type]
  3059. floating-point-overflow
  3060.  
  3061. Errors that occur because of floating-point overflow should inherit from this
  3062. type. This is a subtype of arithmetic-error.
  3063.  
  3064. [Type]
  3065. floating-point-underflow
  3066.  
  3067. Errors that occur because of floating-point underflow should inherit from this
  3068. type. This is a subtype of arithmetic-error.
  3069. [change_end]
  3070.  
  3071. -------------------------------------------------------------------------------
  3072.  
  3073.  
  3074.  
  3075.  
  3076.  
  3077.